1

why if try to consolelog mystate it always gave me the inintial value without change iam tring to fillter the state using fillter but just keep giving the inintial value without any update i try use effect but dosnt work

function Starts() {
  let [mystate, setmystate] = useState(perosns);
  let [temp, setTemp] = useState("");

  useEffect(() => {
    let handler = () => {
      setmystate((prevdate) => {
        return prevdate.filter((Ele) =>  Ele.id !== temp);
      });
      console.log(mystate);
    };
    if (temp) {
      handler();
    }
  }, [temp]);

  return (
    <>
      <input onKeyUp={(e) => setTemp(e.target.value)} />
    </>
  );
}

export let perosns = [
  {
    id: "1",
    first_name: "Mollie",
    last_name: "Keppin",
    email: "mkeppin0@geocities.jp",
    gender: "Female",
    age: 49,
  },...

i tried to add some useEffect but still dosnt work

sife
  • 13
  • 2

1 Answers1

0

It's because when you set a state, it doesn't update immediately. It's asynchronous.

Add this useEffect to log the result

  useEffect(() => {
    console.log(mystate);
  }, [mystate]); // Add this useEffect
Coder Gautam YT
  • 1,044
  • 7
  • 20