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;
{...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
Post a Comment