Trying to make a todo-list app
I want my todo items to have a unique key/id. I want to create the todo item id by taking the last todo item and adding 1 to its id. If the item does not exist it creates a new one with id = 1.
App.js:
import React, {Component} from 'react';
import Todos from './Todos'
import AddTodo from './AddForm'
class App extends Component {
state = {
todos: [
{id: 1, content: 'buy milk'},
{id: 2, content: 'play nintendo'}
]
}
addTodo = (todo) => {
if (this.state.todos[0])
{
todo.id = this.state.todos[this.state.todos.length-1].id +1;
let todos = [...this.state.todos];
todos.push(todo);
this.setState({
todos: todos
});
}
else
{
this.setState({
todos: [
{id: 1, content: todo.content}
]
});
}
console.log(this.state.todos);
}
render() {
return(
<div className="todo-app container">
<h1 className="center blue-text">Todos:</h1>
<Todos deleteTodo={this.deleteTodo} todos={this.state.todos}/>
<AddTodo addTodo={this.addTodo} />
</div>
);
}
}
This almost works. I can add new items if the items have a unique content, but problems occur when the items have the same content.
Unique content console.log:
0: {content: "buy milk", id: 1}
1: {content: "play nintendo", id: 2}
2: {content: "adrfhahhafafdhafhafhhfa", id: 3}
3: {content: "adrfhahhafafdhafhafhhfaafhafhfah", id: 4}
4: {content: "adrfhahhafafdhafhafhhfaafhafhfahafhfahafh", id: 5}
5: {content: "adrfhahhafafdhafhafhhfaafhafhfahafhfahafhafhafhfah", id: 6}
Same content console.log:
0: {content: "buy milk", id: 1}
1: {content: "play nintendo", id: 2}
2: {content: "adrfhahhafafdhafhafhhfa", id: 3}
3: {content: "adrfhahhafafdhafhafhhfaafhafhfah", id: 4}
4: {content: "adrfhahhafafdhafhafhhfaafhafhfahafhfahafh", id: 5}
5: {content: "adrfhahhafafdhafhafhhfaafhafhfahafhfahafhafhafhfah", id: 7}
6: {content: "adrfhahhafafdhafhafhhfaafhafhfahafhfahafhafhafhfah", id: 7}
As you can see the id is the same if the content is the same.
I know that this is probably the wrong way to add id:s in react, but I want to understand what is going on. Thanks in advance.
EDIT: Yevgen pointed out a flaw with my logic if items are deleted, but I am still have not understood why the ID:s are repeating. Someone asked for the rest of the code so I added it all.
AddForm.js
import React, {Component} from 'react';
class AddTodo extends Component {
state = {
content: ''
}
handleChange = (e) => {
this.setState({
content: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
this.props.addTodo(this.state);
}
render(){
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>Add new todo</label>
<input type="text" onChange={this.handleChange} />
</form>
</div>
);
}
}
export default AddTodo;
Todos.js
import React from 'react'
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>
</div>
)
})
) : (
<p className="center">You have no todos left</p>
)
return (
<div className="todos collection">
{todoList}
</div>
)
}
export default Todos;
addTodo. As the array of todos nor a single todo is being passed to theAddTodocomponent, I wonder what are you passing to it when callingaddTodo(?). Could you share that part of the code? Thanks