Reputation: 6353
I have a handleChange that takes an index and value. I want to pass it to a component but I don't want to pass in the index as a prop. I'm trying to return a function that has index already set but not sure how. I tried using bind?
handleChange(index, value) {}
[1,2,3].map((item,index) =>
<component onChange={()=> index => handleChange.bind(this, index)} />
)
Upvotes: 0
Views: 50
Reputation: 78840
Close:
[1,2,3].map((item,index) =>
<component onChange={handleChange.bind(this, index)} />
)
Breaking down what you had before in your {}
, you were passing a function that returns a function instead of just passing a direct function, which .bind(...)
creates for you. An alternative that is cleaner looking, IMO, but does almost the same thing would be:
[1,2,3].map((item,index) =>
<component onChange={value => handleChange(index, value)} />
)
Upvotes: 2