javascript - Using React's shouldComponentUpdate with Immutable.js cursors -


i'm having trouble figuring out how short circuit rendering branch of tree of react components using immutable.js cursors.

take following example:

import react 'react'; import immutable 'immutable'; import cursor 'immutable/contrib/cursor';  let data = immutable.fromjs({   things: [     {title: '', key: 1},     {title: '', key: 2}   ] });  class thing extends react.component {   shouldcomponentupdate(nextprops) {     return this.props.thing.deref() !== nextprops.thing.deref();   }    handlechangetitle(e) {     this.props.thing.set('title', e.target.value);   }        render() {     return <div>       <input value={this.props.thing.get('title')}          onchange={this.handlechangetitle.bind(this)} />     </div>;   } }  class container extends react.component {   render() {     const cursor = cursor.from(this.props.data, 'things', newthings => {       data.set('things', newthings);       rendercontainer();     });      const things = cursor.map(thing => (       <thing thing={thing} key={thing.get('key')} />     ));      return <div>       {things}     </div>;   } }  const rendercontainer = () => {   react.render(<container data={data} />, document.getelementbyid('somediv')); }; 

say change first thing's title. first thing render new title , second thing not re-render due shouldcomponentupdate. however, if change second thing's title, first thing's title go '' since second thing's cursor still pointing @ older version of root data.

we update cursors on each render of container ones don't render due shouldcomponentupdate don't new cursor updated root data. way can see keeping cursors date remove shouldcomponentupdate in thing component in example.

is there way change example use shouldcomponentupdate using fast referential equality checks keep cursors updated?

or, if that's not possible, provide overview of how work cursors + react components , rendering components updated data?

i updated code, see comments inline:

class thing extends react.component {   shouldcomponentupdate(nextprops) {     return this.props.thing.deref() !== nextprops.thing.deref();   }    handlechangetitle(e) {     // trigger method on container handle update     this.props.ontitlechange(this.props.thing.get('key'), e.target.value);   }        render() {     return <div>       <input value={this.props.thing.get('title')}          onchange={this.handlechangetitle.bind(this)} />     </div>;   } }  class container extends react.component {   constructor() {     super();     this.initcursor();   }    initcursor() {     // store cursor instance variable access methods     this.cursor = cursor.from(data, 'things', newthings => {       data = data.set('things', newthings);       // trigger re-render       this.forceupdate();     });   }    render() {     const things = this.cursor.map(thing => (       <thing thing={thing} key={thing.get('key')} ontitlechange={this.ontitlechange.bind(this)} />     ));      return <div>       {things}     </div>;   }    ontitlechange(key, title){     // update cursor store changed things     this.cursor = this.cursor.update(x => {       // update single thing       var thing = x.get(key - 1).set('title', title);       // return updated things       return x.set(key - 1,thing);     });   } }  const rendercontainer = () => {   react.render(<container data={data} />, document.getelementbyid('somediv')); }; 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -