Reactjs 从动态生成的表单中重新生成值
我正在使用react和redux做一个应用程序,我想做一些动态表单,尝试生成输入块,比如todo列表应用程序,下面是一个示例:Reactjs 从动态生成的表单中重新生成值,reactjs,redux,Reactjs,Redux,我正在使用react和redux做一个应用程序,我想做一些动态表单,尝试生成输入块,比如todo列表应用程序,下面是一个示例: <ul> <li> <input type="text" ref="title" name="title" placeholder="title" autofocus/> </li> etc... </ul> 等 但我不知道如何获得这些输入的值,在Redux
<ul>
<li>
<input type="text" ref="title" name="title" placeholder="title" autofocus/>
</li>
etc...
</ul>
-
等
但我不知道如何获得这些输入的值,在Redux中实现这一点的最佳方法是什么?我用
ref
做了很多测试,但是因为我的输入是动态添加的,所以我不知道如何实现这一点。你能帮我吗?除非你必须这样做,否则不要使用refs!我能想到的唯一用例就是聚焦它们,或者做一些模糊的DOM工作
为此,只需做以下事情:
类DynamicInputs扩展了React.Component{
doSomethingWithValue(项,新值){
//在这里,您可以调度一个操作来更新值等。
}
renderItems(){
this.props.items.map(item=>{
返回(
this.doSomethingWithValue(item,ev.target.value)}
);
})
}
render(){
返回{this.renderItems()}
}
}
从“React”导入React,{Component,PropTypes}
导出默认类DynamicInputs扩展组件{
doSomethingWithValue(项,新值){
//在这里,您可以调度一个操作来更新值等。
控制台日志(“项”)
console.log(项目)
console.log(“newValue”)
console.log(newValue)
}
renderItems(){
返回(
{this.props.items.map(item=>
this.doSomethingWithValue(item,ev.target.value)}/>
)}
);
}
render(){
返回(
{this.renderItems()}
);
}
}
dynamicInputs.propTypes={
};
Syntax中有一些小错误,但是这个版本适合我,谢谢好的,谢谢:)我理解这个想法,我会试试:)
import React, { Component, PropTypes } from 'react'
export default class DynamicsInputs extends Component {
doSomethingWithValue(item, newValue) {
// Here you can dispatch an action to update the value, etc.
console.log("item")
console.log(item)
console.log("newValue")
console.log(newValue)
}
renderItems() {
return (
<div>
{this.props.items.map( item =>
<input
value={item.value}
onClick={ev => this.doSomethingWithValue(item, ev.target.value)}/>
)}
</div>
);
}
render() {
return (
<div>
{this.renderItems()}
</div>
);
}
}
DynamicsInputs.propTypes = {
};