Search code examples
javascriptreactjsdictionarychildren

Why does map() get 2 arguments in this case?


Could you help me, please, I can't understand why there are 2 arguments in the map()

Children.map(this.props.children, child =>              
        <button>{child} </button>   

I supposed that this construction should work

Children.map(child =>           
        <button>{child} </button>

But it doesn't.


Solution

  • React.Children map API takes the first argument as the children array to iterate on and the second argument as the callback function that is called with each of the iterated children

    Unlike the normal map which is the prototype of the Array constructor function, in the above case map is a function defined for the Children Object and needs to be passed with the array it has to iterate on