[英]ReactJS Multi Dynamic Input Value
What I need 我需要的
I need to 'build' and 'edit' a form with multiple dynamic input. 我需要使用多个动态输入来“构建”和“编辑”表单。 I have a form, but the user can add other input.
我有一个表单,但是用户可以添加其他输入。
How I do 我怎样做
I already 'build' form before, first i was use the ref
to find my content then in second time i was used the input value
to 'load' my content from the server and added a state
to 'edit' it. 我之前已经“构建”表单,首先我使用
ref
找到我的内容,然后第二次使用input value
从服务器“加载”我的内容,并添加了一个state
来“编辑”它。
The Problem 问题
Now, for an other form, the user can add more input if he want. 现在,对于其他形式,用户可以根据需要添加更多输入。 So for the 'build ' is not a problem I thinks.
因此,对于“构建”而言,我认为不是问题。 And for the 'edit' I don't how many input the user added before, so to added the
input value
for view it's ok, but to edit the value I need a state
and initial it, but i don't know how I can initialized multiple states
dynamically. 对于“编辑”,我没有用户之前添加的
input value
数量,因此可以添加input value
以供查看是可以的,但是要编辑该值,我需要一个state
并将其初始化,但是我不知道如何可以动态初始化多个states
。
My basic code 我的基本代码
@Component = React.createClass
getInitialState: ->
Input1: ''
handelChange: (e)->
@setState
Input1: e.target.value
render: ->
`<form name="form" onSubmit={this.handleSubmit}>
<input className="input" type="text" ref="name" name="name" value={this.state.input1} onChange={this.handelChange}/>
</form>`
(I don't put all the code, but input1 = dataFromServer) (我没有输入所有代码,但是input1 = dataFromServer)
How I can have input2, input3 etc... dynamic ? 我如何才能让input2,input3等...动态?
Thanks for reading! 谢谢阅读!
You won't have several initial state, getInitialState() is called only before the first rendering. 您将没有几个初始状态,只有在第一次渲染之前才调用getInitialState()。
However you can add and complete your state with as many objects as you want even if they were not declared in getInitiaState(); 但是,即使没有在getInitiaState()中声明对象,也可以使用任意数量的对象添加并完成状态。
Lets say the user click on a button to add an input 假设用户单击按钮以添加输入
handelChange: (e)->
var newState = this.state;
newstate["MyNewInput"] = "some initial value field";
@setState
newState
Something like that should work. 这样的事情应该起作用。 Hope it helps, let me know.
希望能有所帮助,让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.