I'm creating a Todo app. Although I'm able to create todos and display them but now with each todo I created a input tag which should change the name of todo.
Here is my container component index.js:
import React, { Component } from "react";
import { render } from "react-dom";
import Hello from "./Hello";
class App extends Component {
state = {
todoItem: [],
todoText: ""
};
addTodoHanlder = event => {
let todoValue = this.state.todoText;
this.setState({
todoItem: [...this.state.todoItem, todoValue]
});
this.clearTodoText();
todoValue = "";
};
clearTodoText() {
this.setState({ todoText: "" });
}
handleChange = event => {
this.setState({ todoText: event.target.value });
};
todoNameChangeHandler = event => {
let todoVal = event.target.value;
this.state.todoItem.map(todo => {
this.setState({
todoItem: todoVal
});
});
};
render() {
return (
<div>
<input
type="text"
placeholder="Enter Todo"
value={this.state.todoText}
onChange={this.handleChange}
/>
<button onClick={this.addTodoHanlder}>AddTodo</button>
<Hello
todos={this.state.todoItem}
changed={this.todoNameChangeHandler}
/>
</div>
);
}
}
render(<App />, document.getElementById("root"));
Here is my Hello.js component to display todos:
import React from "react";
const hello = props => {
return (
<div>
<ul>
{props.todos.map((todo,index) => {
return (
<div>
<p key={index}>{todo}</p>
<input type="text" onChange={props.changed} />
</div>
)
})}
</ul>
</div>
);
};
export default hello;
Can someone help me out how can this be done? I used todoNameChangeHandler method to change the name of todos and I'm getting this error: TypeError props.todos.map is not a function hello /src/Hello.js:7:21 4 | return ( 5 | 6 |
7 | {props.todos.map((todo,index) => { | ^ 8 | return ( 9 | 10 | {todo}
todoNameChangeHandler
have loop with state; because todoItem is array which is changing to string after setState – Piyush Bhati