简体   繁体   中英

react.js: events instead of passing handler in props

In React.js, the parent component passes the handler to update its state via props. In deeply nested components, this can become pretty messy. Is there a way to do this via pub-sub - the parent listens for an event, and executes the handler when it gets fired by the child?

PS: got the idea from Ractive.js ( http://docs.ractivejs.org/latest/components#events ).

I would recommend looking up the flux architecture that facebook uses with their react apps. They use a type of "pub/sub" in singleton stores, and use actions to update the data stores. When an update happens, the store notifies all subscribed controls to update. I've used this setup for a few personal projects, and with great success it has reduced this style of 'mass prop passing' that looks so ugly.

http://facebook.github.io/react/docs/flux-overview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM