javascript - React 15.1.0, Pass entire state from parent to child -


i using react 15.1.0.

suppose there parent "p" component, , contains child component "c".

in older versions of react, when wanted pass entire state child, used {...this.state} , used {this.props.something} child component.

is there simple way in latest latest react 15.1.0 above instance?

note: need pass entire state , not individual props.

 <div>     {react.cloneelement(this.props.children, { title: this.state.title })}  </div> 

what expecting below;

 <div>     {react.cloneelement(this.props.children, {...this.state})}  </div> 

in parent component have below code;

var app = react.createclass({     getinitialstate() {         return{             status: 'disconnected',             title: 'hello world'         }     },     render() {         return(             <div>                 <header title={this.state.title} />                 <div>                     {react.cloneelement(this.props.children, this.state)}                 </div>             </div>         );     } }); 

in child component experimenting using below code.

var speaker = react.createclass({     render() {         return (             <h1>speaker: {this.state.title}</h1>         );     } }); 

but in chrome browser, below result;

enter image description here

{...this.state} 

equals

this.state 

in case. spread operator in es6 (not react-specific feature) ... expands 1 objects' properties parent object, see:

let sampleobject = {   name: 'a_name' };  console.log('non-spread', sampleobject);  // non-spread {name: "a_name"} console.log('spread', {... sampleobject});  // spread {name: "a_name"} 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -