Want to update the state from child in ReactJS? The parent is the source of truth, so the child needs to tell the parent to change its state. Pass a callback from the parent to the child, and have the child call it.
There’s an example on communicating between components in React’s docs that might be helpful too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | A = React.createClass getInitialState: -> mystate: {test: 1} incrementTest: -> @setState {mystate: {test: @state.mystate.test + 1}} render: -> myChild {onChange: @incrementTest, param: @state.mystate} myChild = React.createClass render: -> React.DOM.div {onClick: @change}, @props.param.test change: -> @props.onChange ajax("/..../", JSON.stringify(@props.param)) .done(@onDone).fail(...) onDone: -> console.log "Hum..." |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.