reactjs - How to reuse this little block of code in React.js? -
i'm total react newb, here code have:
var examplecomponent = react.createclass({ getinitialstate: function() { return {close: false}; }, handleclick: function(event) { this.setstate({close: !this.state.close}); }, }); var buttonthing = react.createclass({ <examplecomponent />, render: function() { <div> yo </div> ); } });
where "examplecomponent" can used in multiple places? i've tried have above , putting in "examplecomponent" straight, no luck. or going wrong?
code reuse in react facilitated concept called mixins.
the react docs give concise example.
for case
var examplemixin = { getinitialstate: function() { return {close: false}; }, handleclick: function(event) { this.setstate({close: !this.state.close}); } } var buttonthing = react.createclass({ mixins: [examplemixin], // use mixin render: function() { return (<div> yo </div> ); } });
Comments
Post a Comment