0

I am practicing stuffs in react js like, updating array, removing an array by doing onClick. I've followed some solutions but I didn't get clear idea to achieve to remove an item from an rendered list. I'm having two components 'todoform' and 'todolist'. All the logics are in 'todoform' , I'm rendering the array with help of map in 'todolist'. I didn't able to get ID of the clicked element. Could any one able to correct me , that what I am doing wrong in below code. Thanks in advance.

TododForm:

constructor(props) {
    super(props);
    this.state = {
      InputValue: "",
      todos: [],
    };
    console.log("intialvalue", this.state.InputValue);
  }

  handleChange(e) {
    this.setState({ InputValue: e.target.value });
  }



  handleSubmit(e) {
    e.preventDefault();
    this.setState({
      todos: [
        ...this.state.todos,
        {
          text: this.state.InputValue,
          completed: false,
          id: Math.random() * 1000,
        },
      ],
    });
    this.setState({ InputValue: "" });
  }

  deleteHnadler = () => {
    // console.log("im ckicked", e);
    const rem = this.state.todos.filter((el) => el.id !== this.state.todos.id);
    console.log("object", rem);

    // this.setState({ todos: rem });
  };

  render() {
    return (
      <div className="Todo">
        <form>
          <input
            type="text"
            value={this.state.InputValue}
            onChange={this.handleChange.bind(this)}
          ></input>
          <button onClick={this.handleSubmit.bind(this)}>Add</button>
        </form>
        <p>{this.state.InputValue}</p>
        <TodoList todos={this.state.todos} clickMe={this.deleteHnadler} />
      </div>
    );
  }

Todolost.js
 clickMe = () => {
    this.props.clickMe();
  };

  render() {
    return (
      <div>
        {this.props.todos.map((todo) => (
          <li key={todo.id} todo={todo}>
            {todo.text}
            <button>Completed</button>
            <button onClick={this.clickMe}>Delete</button>
          </li>
        ))}
      </div>
    );
  }
2
  • 1
    Start with onClick={() => this.props.clickMe(todo.id)} Commented Feb 15, 2021 at 17:43
  • Thanks, for the quick response, when I am using above code, while doing console I can see the whole array is getting printed. I am using filter in const rem , while consoling rem I can see all the array objects are getting printed instead of clicked item. Commented Feb 15, 2021 at 17:53

1 Answer 1

2

In TodoList.js send the id in the event back to the parent component.

clickMe = (id) => {
  this.props.clickMe(id);
};

<button onClick={() => this.clickMe(todo.id)}>Delete</button>

In TodoForm.js receive that id and remove the item from array.

deleteHnadler = (id) => {
   let todos = this.state.todos.filter((el) => el.id !== id);
   this.setState({ todos })
};

<TodoList todos={this.state.todos} clickMe={this.deleteHnadler} />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.