0

No1)

  const handleCheckList = (id, checked,findCost) => {
    if (checked) {
      setCheckLists([...checkLists, id]);
    } else if (!checked) {
      // checkLists에서 입력된 id랑 같은걸 찾아서 같지 않은거만 filter
      const tmp = checkLists;
      const newarr = tmp.filter((it) => it !== id);
      setCheckLists(newarr);
    }
    findCost();<< Here!! 
  };

No2)

  const findCost = () => {
    for (let i = 0; i < checkLists.length; i++) {
      const tmparr = cart;
      const findobj = tmparr.find((it) => it.id == checkLists[i]);
      console.log(findobj);
    }
  };

I want to run the findCost function after completing the setState operation in the handelCheckList function. However, setState is asynchronous, so the previous value is passed to findCost. How can I run findCost after the setState runs completely?

new promise(){ if function... }.then(findCost())

--> but the result is not good... :(

0 Answers0