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?