4 ms·
I have been trying to use React in an application I am building. One recurring issue I have with React is child to parent component communication. The recommend
by bgdam 12y ago
I have been trying to use React in an application I am building. One recurring issue I have with React is child to parent component communication. The recommended way seems to be to have the child component emit events which are then caught and handled by the parent component.
Unfortunately, this approach does not work very well in the cases where I have multiple instances of the same component, in which case all the parents will be recieving and handling the event emitted by a single child. I would love to hear any possible solutions for this that do not involve either registering unique event names for each instance of the component or passing in a unique id as part of the event payload, which is then verified by the handler function.
- insin 12y agoThe docs recommend passing a callback for parent-child communication: http://facebook.github.io/react/tips/communicate-between-components.html http://facebook.github.io/react/tips/communicate-between-com...
- loganfsmyth 12y agoCould you elaborate here on what you mean by "all the parents will be recieving and handling the event emitted by a single child"? Or perhaps post a question on StackOverflow.
- theycallmemorty 12y agoMaybe I'm not understanding the problem correctly, but I simply had the child components 'know' their index or id (stored in props) and then pass that to the parent with any call.