javascript - Unable to change state of checkbox dynamically using setState -
below simple react component initial state of checkbox false.
i trying change dynamically using setstate(). not work.
here code:
var hello = react.createclass({ getinitialstate: function(){ return { checked : this.props.checked.tostring() === "false" ? false : true }; }, render: function() { console.log("rendering=="); return <div><input type = "checkbox" defaultchecked = {this.state.checked}/></div>; } }); var compref = react.render(<hello checked = "false" />, document.body); trying change state after rendering component settimeout(function(){ compref.setstate({checked: true}) },3000); i unable change checkbox state using setstate.
here fiddle
by providing defaultchecked instead of checked creating uncontrolled component. uncontrolled means can't control changing defaultchecked. change checkbox's state code, need provide checked it.
here updated fiddle: http://jsfiddle.net/p4ps7nob/
the other thing consider is: props in getinitialstate anti-pattern
Comments
Post a Comment