NVI*_*NVI 17 javascript data-binding reactjs
我的状态是:
[
{type: "translateX", x: 10},
{type: "scaleX", x: 1.2}
]
Run Code Online (Sandbox Code Playgroud)
我正在使用双向绑定助手,我无法提供有效的密钥字符串linkState
:
this.state.map(function(item, i) {
return <div><input valueLink={this.linkState( ??? )}></div>
}
Run Code Online (Sandbox Code Playgroud)
如果this.linkState
接受一些查询语法会很好,例如从我的示例中"0.type"
检索 "translateX"
.
有没有解决方法?
我编写了DeepLinkState mixin,它是React.addons.LinkedStateMixin的直接替代品.用法示例:
this.state.map(function(item, i) {
return <div><input valueLink={this.linkState([i, "x"])}></div>
}
Run Code Online (Sandbox Code Playgroud)
linkState("0.x")
也是可以接受的语法.
tun*_*ngd 19
编辑:
我意识到深度路径LinkedState
非常酷,所以我尝试实现它.
代码:https
://gist.github.com/tungd/8367229用法:http://jsfiddle.net/uHm6k/3/
正如文件所述,它LinkedState
是一个包装器onChange/setState
,用于简单的情况.你总是可以写完全onChange/setState
来实现你想要的.如果你真的想坚持下去LinkedState
,你可以使用非mixin版本,例如:
getInitialState: function() {
return { values: [
{ type: "translateX", x: 10 },
{ type: "scaleX", x: 1.2 }
]}
},
handleTypeChange: function(i, value) {
this.state.values[i].type = value
this.setState({ values: this.state.values })
},
render: function() {
...
this.state.values.map(function(item, i) {
var typeLink = {
value: this.state.values[i].type,
requestChange: this.handleTypeChange.bind(null, i)
}
return <div><input valueLink={typeLink}/></div>
}, this)
...
}
Run Code Online (Sandbox Code Playgroud)
这是JSFiddle的工作:http://jsfiddle.net/srbGL/