2
I’m making a whole app list, and I’d like to have the Edit button appear in the field where I add my new item. But the action of the button and the state are sibling components.
Addtodo (as I pass the state of Addtodo to Todo ?)
import React, { Component } from "react";
export default class AddTodo extends Component {
state = {
content: ""
};
handleChange = e => {
this.setState({ content: e.target.value });
};
handleSubmit = e => {
e.preventDefault();
this.props.addTodo(this.state);
this.setState({ content: "" });
};
// Como passar o estado dessa função ?
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label class="blue-text">Add new todo</label>
<input
type="text"
onChange={this.handleChange}
value={this.state.content}
/>
</form>
</div>
);
}
}
All
import React from "react";
import "./style.css";
const Todos = ({ todos, deleteTodo }) => {
const todoList = todos.length ? (
todos.map(todo => {
return (
<div className="collection-item" key={todo.id}>
<span
onClick={() => {
deleteTodo(todo.id);
}}
>
{todo.content}
</span>
<button
class="btn-small orange lighten-1 right"
id="btnEdit"
onClick={() => {editTodo(todo.content)}}
>
<i class="material-icons">edit</i>
</button>
</div>
);
})
) : (
<p className="center">Nothing todo, yay</p>
);
return <div className="todos collection">{todoList}</div>;
};
export default Todos;
App
import React, { Component } from "react";
import Todos from "./Todos";
import AddTodo from "./AddTodo";
export default class App extends Component {
state = {
todos: [
{ id: 1, content: "learning Redux" },
{ id: 2, content: "buy some milk" },
{ id: 3, content: "play Mario kart" }
]
};
deleteTodo = id => {
const todos = this.state.todos.filter(todo => {
return todo.id !== id;
});
this.setState({ todos });
};
addTodo = todo => {
todo.id = Math.random();
const todoss = [...this.state.todos, todo];
this.setState({ todos: todoss });
};
render() {
return (
<div className="todo-app container">
<h1 className="center blue-text">Todo list</h1>
<Todos todos={this.state.todos} deleteTodo={this.deleteTodo} />
<AddTodo addTodo={this.addTodo} />
</div>
);
}
}
You can only pass props from parent components to child components, directly between siblings is not possible, in which case you would have to go from one child, to father and father to another child.
– Rafael Costa
True, I’m putting in the state of the App an array to receive the status of the child, since App is the parent, now we need to do a function that passes this state to Addtodo child, because this Addtodo is an insert capo, the idea is to put the text right there. Got confused ?
– Beatriz Cibele
Study Redux or Context of
Reactjs
– novic
No, that’s the idea. I think there’s a way to get through it, but using Hooks, I think Usecontext does this, it goes straight to any component without taking into consideration it was hierarchy. But since you are using Class Components I believe that only so, from father to son.
– Rafael Costa
You can control the state in the father and pass a function of
criarTodo
for a child and thelistaDeTodos
for the other son. Worth reading When to use context? and Before you use context.– Rafael Tavares
I made an example with
Context
but it’s with React Hooks– novic