0

I have onClick event, that is a trigger for couple functions. FunctionsetRcomForApi(rcom) sets valuesip1, port1 for function getApiVersion(ip1, port1). The problem is - on first click ip1, port1 are undefined. I've tried to use callback - but it didn't work...

 const onClick = () => {
        setIndexPpk(index);
        setRcomForApi(rcom);   //sets ip1 and port1 in Context State.    
        getApiVersion(ip1, port1);  // on first click both undefined          
    };

2
  • Have you tried using a Promise ? Also, are ip1, port1 both stateful values? Commented Mar 3, 2020 at 12:29
  • Simply add a condition to check if needed would be good enough Commented Mar 3, 2020 at 12:38

1 Answer 1

1

This is because setRcomForApi(rcom) is asynchronous. So you need to use an useEffect to check for changes in values of ip1 and port1

 useEffect(() => {
if(ip1 && port1) {
    getApiVersion(ip1, port1);
}
}, [ip1, port1]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.