javascript - ReactJS data flow with deep hierarchical data -


i'm exploring reactjs , trying grasp of core concepts. started piecing prototype of application working on has following hierarchy of

  • customer
    • locations
      • addresses
      • contacts

the page working on input form customer , of related children. each of these sections have text inputs house data, seemed natural place house hierarchy of components.

from have read reactjs, if going manage state, should in common ancestor of controls. means changes in child should bubble event keeper of state handle changes. should update state , changes re-rendered. makes sense in simple scenarios, brings me more complicated hierarchy.

  • if change happens down in 1 of many addresses, supposed bubble event location customer?
  • if so, best way tell state specific address changed?
  • if have call through each level of hierarchy, wouldn't make lot of boilerplate propagate simple change?
  • should attaching onchange event on each text box, or should wait until submit form gather data?

react talks reactlink (https://facebook.github.io/react/docs/two-way-binding-helpers.html) way manage more complex data binding, doesn't give great example of how manage larger hierarchy. also, states application shouldn't need this. well, application isn't complicated, couple nested controls shared state. react supposed shine, wouldn't think jump edge case solutions.

i faced same puzzle - react docs not complete in area. 1 way found work:

var parent = react.createclass({     mixins: [react.addons.linkedstatemixin],      getinitialstate: function() {         return {             p: 'hello world',             q: 'click me if i\'m pink!',             r: 'a'         };     },     render: function() {         return (             <div classname='parent'>                 <p>                     <h3>parent</h3>                     <tt>                         p: {this.state.p}<br />                         q: {this.state.q}<br />                         r: {this.state.r}                     </tt>                 </p>                  <child q = {this.linkstate('q')}                        p = {this.linkstate('p')}                        r = {this.linkstate('r')} />             </div>         );     } }); var child = react.createclass({     clicked: function() {         this.props.q.requestchange("thank :)");     },     render: function() {         return (             <div classname='child'>                 <h3>child</h3>                  <p>                     <input valuelink={this.props.p} />                 </p>                  <p>                     <tt onclick={this.clicked}>                         {this.props.q.value}                     </tt>                 </p>                  <select valuelink={this.props.r}>                     <option value={'a'}>a</option>                     <option value={'b'}>b</option>                 </select>             </div>         );     } });  react.render(<parent />, document.body) 

https://jsfiddle.net/cachvico/p81s75x5/

if has improvements i'd glad hear them!


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -