単純なToDoアプリケーションを作成しましたが、App(InputForm)の子コンポーネントの入力値にアクセスする際に問題があります。React.js子コンポーネントの入力値にアクセスする方法
多分私はそれが動作するように構造やロジックを再構築する必要がありますか?
はここに私のアプリケーションコンポーネントです:助けを
class InputForm extends React.Component {
render(){
return (
<form onSubmit={this.props.onSubmit}>
<input
ref="todoInput"
type="text"
placeholder="Type your text here" />
<button type="submit">Add to list</button>
</form>
)
}
}
ありがとう:
class App extends React.Component {
constructor(){
super();
this.state = {
items : []
}
}
addTodo (e){
e.preventDefault();
let itemHeading = this.refs.todoInput.value; // TODO Access to input value
let itemKey = Date.now();
const items = this.state.items.slice();
items.push({
heading: itemHeading,
key: itemKey
})
this.setState({items: items});
}
render() {
return (
<div className="app-container">
<InputForm onSubmit={this.addTodo.bind(this)}></InputForm>
<TodoItems entries={this.state.items} />
</div>
);
}
}
ここに私のInputFormではコンポーネントです。
どのコンポーネントのコンポーネントに値を渡したいですか? – Codesingh
@Codesingh 'InputForm'からの入力値を渡し、' App'コンポーネントの 'addTodo'関数でそれを使用したいと思います。私はそれが必要な行にTODOのコメントを見ることができます。 –