Im new to react native. when I receive data from server and set the data to variable in alert nothing to show for me can help me to fix this? tank you
const [result, setResult] = useState('')
Server(mydata).then(data => {
//data value is ok
//when set data value to setResult
//dose not show anything in alert
setResult(data.data)
alert(setResult)
})
Because setResult() is an async operation, you should be using useEffect to wait for the result and make the alert. Something like this:
const [result, setResult] = useState('')
Server(mydata).then(data => {
//data value is ok
//when set data value to setResult
//dose not show anything in alert
setResult(data.data)
})
useEffect(() => {
alert(result)
}, [result])