Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

too many re re-renders error on remove todo

import React, { useState, useEffect } from "react";
import "./style.css";

function App() {
  const [movieData, setMovieData] = useState();
  const [directorData, setDirectorData] = useState();
  const [listData, setListData] = useState([]);

  const submitHandler = () => {
    setListData([
      ...listData,
      {
        movie: movieData,
        director: directorData,
      },
    ]);
    setMovieData("");
    setDirectorData("");
  };
  const removeHandler = (id) => {
    const newlist = listData.filter((remId) => {
      return remId !== id;
    });
    setListData(newlist)
  };
  return (
    <div className="App">
      <div>
        <h1>Todo App</h1>
      </div>
      <div className="form">
        <div>
          <label>Movie</label>
          <input
            type="text"
            placeholder="type items"
            value={movieData}
            onChange={(e) => setMovieData(e.target.value)}
          />
        </div>
        <div>
          <label>Director</label>
          <input
            type="text"
            placeholder="type items"
            value={directorData}
            onChange={(e) => setDirectorData(e.target.value)}
          />
        </div>
        <div>
          <button onClick={submitHandler}>Submit</button>
        </div>
      </div>
      <div>
        {listData.map((item, index) => {
          return (
            <li key={index}>
              <span>{item.movie}</span>, <span>{item.director}</span>{" "}
              <span style={{ marginLeft: "2rem" }}>
                <button onClick={removeHandler(index)} style={{ color: "red" }}>
                  X
                </button>
              </span>
            </li>
          );
        })}
      </div>
    </div>
  );
}

export default App;

As soon as I added removeHandler, I am getting too many re renders onSubmit. Its working fine once I remove removeHandler.

As soon as I added removeHandler, I am getting too many re renders onSubmit. Its working fine once I remove removeHandler.
As soon as I added removeHandler, I am getting too many re renders onSubmit. Its working fine once I remove removeHandler.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Change to onClick={() => removeHandler(index)}

and you also have to remove ‘return’ in your array.filter

      remId !== id;
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading