0

I have List Component to show the list of data, in each row I have edit button, when user click on edit button, child component open and ready for update, the problem is when user update row list doesn't update
this is the handleAcceptButton in List component

const [forceUpdate, setForceUpdate ] = React.useState(false);

const handleAcceptClose = async() => {
    const temp = await childRef.current.checkWhatIs();
    console.log('---befor--setForceUpdate-----',forceUpdate)
    setForceUpdate(!forceUpdate)
    console.log('---after--setForceUpdate-----',forceUpdate)
};

this is the useEffect for get the List data

useEffect(() => {
    async function getData(urlMe) {
        await axios
        .get(`${BaseURL}/helpSeekers/helpSeekersListAll`)
        .then((response) => {
            setData(response.data.result.data);
            setlinkPages(response.data.result.links);
            setForceUpdate(false); //// this is for reloading
        });
    }
    if (loadingNextData) {
        getData(urlMe);
    }
}, [forceUpdate])

when user Update the the row and click on the handleAcceptButton the useEffect doesn't rerender

  • 1
    `setState` is **asynchronous**! – dbuchet May 10 '21 at 16:40
  • @dbuchet I know but how can I fix this – miladjurablu May 10 '21 at 16:42
  • My point is `console.log('---after--setForceUpdate-----',forceUpdate)` will never show you the updated value, but the previous one. You don't need to have a `forceReload` state, any setState will trigger a component re-render. I'm afraid you don't provide enough code to really understand what's your problem. Mayve an exemple on codesandbox would help – dbuchet May 10 '21 at 16:48
  • Does this answer your question? [setState doesn't update the state immediately](https://stackoverflow.com/questions/41278385/setstate-doesnt-update-the-state-immediately) – ggorlen May 10 '21 at 17:32
  • @miladjurablu this isn't a problem that requires fixing, this is the intended behavior of React. If you need to log the value, do so on the next rerender or by passing a callback as a second parameter to `setState`. `setState` doesn't say "update the value immediately to be visible on the next line of code", it says "on the next render of the component, use this new value". State variables are effectively immutable. – ggorlen May 10 '21 at 17:32

0 Answers0