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

Does setState or dispatch (useReducer hook) make component re-render before calling next lines?

$
0
0

I realize a problem when using useState and useReducer hook that any lines of code after the update state function (setState, dispatch) will be called in the next re-rendering (with its previous state before update). It means that update state function causes re-rendering immediately and not waiting the whole function executed.

const [aaa, setAAA] = useState<boolean>(false);const updateMyBiddingList = async (atDate?: string) => {    try {      console.log('step 0');      const result = await getBiddingCartFromService(atDate ? atDate : myBiddingListState.myBiddingList[0].updatedAt);      if (result.responseCode.toString().startsWith('2')) {        setAAA(true);        console.log('step 1');      }      console.log('step 2 ', aaa);    }    catch (err) {      if (timeOut.current) clearTimeout(timeOut.current);      timeOut.current = setTimeout(() => updateMyBiddingList(), TIMEOUT);    }  }console.log('Component is re-rendering... ', aaa);return ...

The above codes will log in the following order:

  • step 0
  • Component is re-rendering... true
  • step 1
  • step 2 true

Does anyone explain the workflow of update state hook for me? Thank in advance.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



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