、私は1つがあなたは彼らが部品を制御されるように動的にremove
とadd
input
要素にできるようになるするform
を実施することになっているか疑問に思って?これも可能ですか?私たちが見ることができるの例でReact.JSで制御されたコンポーネントを使用して動的フォームを実装する方法は?私は<code>controlled form components</code><a href="https://facebook.github.io/react/docs/forms.html#controlled-components" rel="noreferrer">in react.js official website</a>のための参照の例を見ていたよう
:
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
により私の仕事の性質のために、私はしばしば自分自身がそのようなフォームを実装する必要が見つけます。さらに、私はadd
またはremove
input
の要素を直接的には管理していませんが、ここでは単純化のために基本的なフォーム要素を求めています。
これは良い解決策のようです。ちなみに、はい、私のトラブルはイベントのバインディングで...ありがとう! –