0

I know hooks are asynchronous and so I'll need to use useEffect() hook along with dependency array param in order to grab the counter's value that's being updated in real time.

However, I need to use these values in the counter() function and not in useEffect().

I can see the values being updated in useEffect() correctly but how can I grab those updated values in real time in counter() in order for me to use values?

I've tried (to no avail): Passing liked and disliked as params in the counter(liked, disliked) function but that didn't help

const [liked, setLiked] = useState(0);
const [disliked, setDisliked] = useState(0);

useEffect(() => {

    // Correct values are being logged
    console.log("liked", liked);
    console.log("disliked", disliked);
}, [liked, disliked]);

const counter = () => {
    if(someCondition.........) {
        setDisliked(disliked + 1);
        handleLike();
    } else {
        setLiked(liked + 1);
        handleDislike();
    }
};

const handleLike = () => {
    const url = 'http://127.0.0.1:8000/api/like';

    const headers = {
        "Accept": 'application/json'
    };

    let data = {
        'liked' : liked // value updates on second click only
    };

    console.log(data);

    axios.post(url, data, {headers})
        .then(resp => {
            console.log(resp.data);
        }).catch(err => {
        console.log(err);
    });

};

const handleDislike = () => {
    const url = 'http://127.0.0.1:8000/api/dislike';

    const headers = {
        "Accept": 'application/json'
    };

    let data = {
        'disliked' : disliked // value updates on second click only
    };

    console.log(data);

    axios.post(url, data, {headers})
        .then(resp => {
            console.log(resp.data);
        }).catch(err => {
        console.log(err);
    });

};

return(
   <button onClick={counter}>Click Here</button>
);
sp92
  • 873
  • 1
  • 6
  • 17
  • 1
    It's difficult to understand the issue. "_but how can I grab those updated values in real time in counter_" - those values are available in whole component scope. You always can access them as you said - _in real time_. – kind user Nov 20 '22 at 19:50
  • @kinduser It's not though. If I use `liked` or `disliked` variables in any other function, it updates after the second click only. – sp92 Nov 20 '22 at 19:58
  • That's not true. Even if you are not using the callbacks when setting the new state, the value should be updated immediately after changing the state. Do you have some minimal reproducible example of your issue like codesandbox or stackblitz? – kind user Nov 20 '22 at 20:04
  • @sp92 Please show us how you were trying to use those variables in the `counter` function – Bergi Nov 20 '22 at 20:15
  • @Bergi I've updated my original post in an attempt to clearly show what I'm experiencing. – sp92 Nov 20 '22 at 20:31
  • Ah, yes, this is to be expected. Calling `setDisliked` only changes the state of the component and re-renders it with the new state, it does not reassign to the current `const disliked`. You can either pass the new value `disliked + 1` explicitly into `handleLike`, or you execute that on an effect (e.g. `useEffect(handleLike, [disliked]); useEffect(handleDislike, [liked]);`). – Bergi Nov 20 '22 at 20:39

1 Answers1

0

setters should be like this

setDisliked((disliked) => (disliked+1))
Tarmah
  • 139
  • 6