Skip to content

How does the third argument of React.Children.forEach work? #4339

Closed
@VaJoy

Description

@VaJoy

I've found out that React.Children.map/forEach has an optional argument as [, object thisArg] which could set this to another object through Top-Level API, then I try to run the code below:

        var obj = {
            num : 3
        };

        var Component = React.createClass({
            deal : function(){
                console.log(this, this.num);  //it`s NOT obj
            },
            render : function() {
                React.Children.forEach(this.props.children, this.deal, obj);
                return (<ul>
                          {this.props.children}
                        </ul>)
            }
        });

        React.render(
            (
                <Component>
                    <li>0</li>
                    <li>1</li>
                    <li>2</li>
                </Component>
            ), document.body
        )

it logs three times of Constructor undefined and I have no idea why they are not obj 3.

so how does the third argument of React.Children.forEach work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions