flux icon indicating copy to clipboard operation
flux copied to clipboard

Third party components and flux architecture

Open istarkov opened this issue 11 years ago • 4 comments

For example i have some third-party component - Map. Map has marks. And i want to show some balloon with information from server when mark click event occurs.

No flux pseudo code looks like:

marks.on('click', mark_id => 
    server.getmarkinfo(mark_id)
      .then(data => marks.show_balloon(mark_id, data)));

In flux i call action, action makes call to server and dispatch some event to store when data available, store save data and call on_change event, jsx component receive on_change event, get data and mark_id from store.

What is recommended way to call ONCE marks.show_balloon(mark_id, data). The only way i see for one time calls, i need to have some kind of state at jsx component level, in this situation is something like marks.is_ballon_open(mark_id) || marks.show_balloon(mark_id, data) .

istarkov avatar Jan 07 '15 20:01 istarkov

Yes, component state is different from application state, and React components should maintain component state when necessary. Your example seems like a good candidate for maintaining a little bit of component state. However, if any other component needs to know whether the balloon is open, it becomes application state, and then it should be maintained in a store.

fisherwebdev avatar Jan 09 '15 18:01 fisherwebdev

Thank you for your answer. I've just found some interesting way to maintain such component level state using react. So my problem was how to call show_balloon on specific mark_id once and only once. The idea is to use react ability to maintain keyed children collection elements. For example if i create some react component

var Marker = React.createClass({
  componentDidMount() {
    var p = this.props;    
    p.api.show_balloon(p.mark_id, p.data)
  },
  componentWillUnmount() {
    var p = this.props;
    p.api.close_balloon(p.mark_id);
  },
  shouldComponentUpdate() {
    return false;   
  },
  render () {
    return null;
  }
});

and use this component from some jsx render as collection element

render () {
  <map>
     {markers
       .filter(m => m.has_balloon)
       .map(m => <Marker key={m.mark_id} api={api} {...m} />)} 
  </map>
}

I get once and only once call to p.api.show_balloon for each marker with has_balloon set to true in markers collection (also destruction code p.api.close_balloon will work as has_balloon become false).

For me it looks like implicit state of component, and looks more funny than explicit call to marks.is_ballon_open(mark_id)

But i don't sure would react use key attribute for maintain collection element state in the future.

istarkov avatar Jan 10 '15 07:01 istarkov

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

Looks like bill mostly answered this.

kyldvs avatar Nov 18 '15 21:11 kyldvs