I wrote simple todo app, but now I have troubles with accessing to value of input in child component for App (InputForm).
Maybe I need to rebuild structure or logic somehow to make it works ?
Here's my App component:
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>
);
}
}
Here's my InputForm component:
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>
)
}
}
Thanks for help.
InputFormand use it inaddTodofunction inAppcomponent. You can see TODO comment on line where I need it.