Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to Queuing Items to be added into React Native Context's State

$
0
0

I'm trying to show logs like console.log in my app UI using React Native Context. I have this codes in the context:

...const [logs, setLogs] = useState<string[]>([]);const addToConsoleLogs = (log: string) => {  setLogs([...logs, log]);};...

and in the screen when I call that function before and after a fetch:

...doFetch = () => {  addToConsoleLogs('Start..');  await fetch(...);  addToConsoleLogs('Done.');}...

is not working as expected, because this part >> setLogs([...logs, log]);, the previous logs state value seems not actual in the second call because I think those calls are in the same thread and they're asynchronous. Too bad we can't put await in front of the setLogs(). What is the best way to mimic console.log in React Native Hooks mechanism?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>