[英]How to distinguish different form inputs in React?
I have two form inputs that I want to have for name and email. 我想为姓名和电子邮件输入两个表单。
I'm following examples from the React docs ( https://facebook.github.io/react/docs/forms.html ), but I'm not sure how I can distinguish between the two different forms. 我正在跟踪React文档( https://facebook.github.io/react/docs/forms.html )中的示例,但是我不确定如何区分这两种不同的形式。 When I type in the first or second form, the other form receives
this.state.value
as well. 当我输入第一种或第二种形式时,另一种形式也将接收到
this.state.value
。
constructor(props) {
super(props);
this.state = {
value: '',
}
}
handleChange(e) {
this.setState({
value: e.target.value,
})
}
input() {
return (
<form>
<input type="text" value={this.state.value} onChange={this.handleChange.bind(this)}/>
<input type="email" value={this.state.value} onChange={this.handleChange.bind(this)}/>
</form>
)
}
I'm using ES6. 我正在使用ES6。 Do I need some sort of
id
to distinguish between the two forms? 我是否需要某种
id
来区分这两种形式? I tried to have a different value in the constructor (ie email = ''
) but it yielded the same results. 我试图在构造函数中使用一个不同的值(例如
email = ''
),但结果相同。
Use a id
for each <input/>
and while saving it in the state
, use the id
as the key
and the input value as the value
. 为每个
<input/>
使用一个id
,并在将其保存为state
,使用id
作为key
并使用输入值作为value
。
[Update] [更新]
As mentioned in the comments below, I've added the ability to generate unique id
s for each instances. 如以下注释中所述,我添加了为每个实例生成唯一
id
的功能。
Hope this helps! 希望这可以帮助!
class App extends React.Component{ constructor(props) { super(props); this.ids = { textInput: Math.random().toString(36).slice(-5), emailInput: Math.random().toString(36).slice(-5), } this.state = {} } handleChange(e) { this.setState({ [e.target.id]: e.target.value, }) } input() { return ( <form> <input type="text" value={this.state[this.ids.textInput]} id={this.ids.textInput} onChange={this.handleChange.bind(this)}/> <input type="email" value={this.state[this.ids.emailInput]} id={this.ids.emailInput} onChange={this.handleChange.bind(this)}/> </form> ) } render(){ console.log(this.state) return this.input() } } ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>
constructor(props) {
super(props);
this.state = {
textValue: '',
emailValue: ''
}
}
handleTextChange(e) {
this.setState({
textValue: e.target.value
})
}
handleEmailChange(e) {
this.setState({
emailValue: e.target.value
})
}
input() {
return (
<form>
<input type="text" value={this.state.textValue} onChange={this.handleTextChange.bind(this)}/>
<input type="email" value={this.state.emailValue} onChange={this.handleEmailChange.bind(this)}/>
</form>
)
}
You should store email & value in separate states. 您应该在单独的状态下存储电子邮件和值。
this.state = {
text: '',
email: ''
}
And you should change a bit handleChange
method 而且你应该改变一点
handleChange
方法
handleChange(e) {
this.setState({
[e.target.type]: e.target.value,
})
}
Hope it will help you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.