javascript - Reactjs passing methods as props to child -
is there way pass methods of current class child class props
as example;
var signupmodal = react.createclass({ mixins: [checkmixin], getinitialstate: function(){ return { 'wizardstate': 0 } }, setwizardstate: function(state){ var = this; switch (state){ case 0: that.setstate({'wizardstate': 0}); break; case 1: that.setstate({'wizardstate': 1}); break; case 2: that.setstate({'wizardstate': 2}); break; case 3: that.setstate({'wizardstate': 3}); break; case 4: that.setstate({'wizardstate': 4}); break; } }, render: function(){ var temp; switch (this.state.wizardstate){ case 0: temp = (<signupsetup setwizardstate={this.setwizardstate}/>); break; case 1: temp = (<emailsetup />); break; case 2: temp = (<passwordsetup />); break; case 3: temp = (<usernamesetup />); break; case 4: temp = (<categoriessetup />); break; } return (<modal {...this.props} title="login" animation={false}> <div classname="modal-body"> <div> {temp} </div> </div> </modal>) var signupsetup = react.createclass({ render: function(){ return (<button onclick={this.props.setwizardstate(1)}></button>) } });
i want pass setwizardstate method of signupmodal child signupsetup prop, error
uncaught error: invariant violation: replacestate(...): cannot update during existing state transition (such within
render). render methods should pure function of props , state.react.js:17122
the problems here:
<button onclick={this.props.setwizardstate(1)}></button>
first typo (onclick, capital c). main problem you're calling setwizardstate, , onclick takes function. need partially apply it.
onclick={this.props.setwizardstate.bind(null, 1)}
Comments
Post a Comment