我正在使用ReactJS和CoffeeScript(:
实际上,我有一个处理状态的组件A。状态字段传递给子组件(在此示例中称为myChild)。子组件需要更新来自父状态的一些值。
如何用ReactJS的方式来做到这一点?
我正在使用ReactJS和CoffeeScript(:
实际上,我有一个处理状态的组件A。状态字段传递给子组件(在此示例中称为myChild)。子组件需要更新来自父状态的一些值。
如何用ReactJS的方式来做到这一点?
A = React.createClass
getInitialState:
mystate: {test: 1}
render: ->
myChild {param: @state.mystate}
myChild = React.createClass
render: ->
React.DOM.div {onClick: @change}, @props.param.test
change: ->
@props.param.test += 1 # WRONG WRONG WRONG
ajax("/..../", JSON.stringify(@props.param))
.done(@onDone).fail(...)
onDone: ->
console.log "Hum..."
评论:
-@props.param.test不能这样更改(为了一致性,它应该是不可变的)。
-@props.param实际上是组件A状态内部的对象,因此应该使用@setState进行更新,但由于我们在子级中,所以无法实现:(
如何清除这个问题,以获得良好的ReactJS组件?
保罗?你还会帮助我吗?:D