javascript - How to transfer a value/object bottom to top in reactjs -


reactjs makes simple data injection, can inject data via parent element throught this.props or this.state objects. transfer value bottom top, can specify ref attribute , use dom methods or edit it.

what if want transfer object thus:

var editor = react.createclass({   componentdidmount: function() {     var editor = new codemirror(this.getdomnode());     this.editor = editor;   },    render: function() {     return (       <div classname="editor"></div>     );   } });  var app = react.createclass({   render: function() {     return (       <editor />       <button onclick={this._save}></button>     );   },    _save: function() {     // i'm confused how can `editor` object?   } }); 

i did thought transfer through attribute like: data-editor, if there elegant or recommend way in reactjs handle such situations?

thanks in advance.

there multiple ways this. 1 assign ref element , expose method editor or value:

var editor = react.createclass({   componentdidmount: function() {     var editor = new codemirror(this.getdomnode());     this.editor = editor;   },    render: function() {     return (       <div classname="editor"></div>     );   }    geteditor: function() {     return this.editor;   } });  var app = react.createclass({   render: function() {     return (       <editor ref="editor"/>       <button onclick={this._save}></button>     );   },    _save: function() {     var editor = this.refs.editor.geteditor();   } }); 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -