2
I’m trying to do a todolist with React and at the moment I have two components: Todolistapp and Todoform. In Todolistapp I have a function that adds a value in a list that is in the state, but this value comes from another component: Todoform.
I’ll put the code of both:
import React from 'react';
import TodoForm from '../components/TodoForm';
export default class TodoListApp extends React.Component {
constructor(props) {
super(props);
this.state = {
items: []
}
}
render() {
return (
<TodoForm pushToItems={this.pushToItems}></TodoForm>
);
}
//Tento passar esta função para o outro componente
pushToItems = (todo) => {
this.setState({
items: [...this.state.items, todo]
});
}
}
Presentation component:
import React from 'react';
export default class TodoForm extends React.Component {
constructor(props) {
super(props);
this.state = {
todo: ''
}
}
handleChange = (event) => {
this.setState({
[event.target.name]: event.target.value,
});
}
//Chamo a função que vem do outro componente através das props.
onAdd = () => {
this.props.pushToItems(this.state.todo);
}
render() {
return (
<div>
<input type="text" name="todo" placeholder="Enter todo here"
onChange={this.handleChange}></input>
<button type="button" onClick={this.onAdd}>Add</button>
</div>
);
}
}
What’s happening is that when I click on the add a whole button, it returns to me:
Uncaught Typeerror: this.props.pushToItems is not a Function
As if the todoform component is not receiving the function as prop.
Does anyone know what it can be?
I couldn’t reproduce the problem. I created a Codesandbox, but it’s working...
– Luiz Felipe
@Luizfelipe And here it continues.... https://prnt.sc/obggs6
– Naldson