How to manage 'data is undefined'
I am working on a migration from redux to RQ. Our existing codebase is full of useEffect's which RQ is slowly replacing which is great, however, there are some instances where the app is breaking as the data is rendering as undefined but useEffect is expecting the value ( I agree we are using useEffect incorrectly probably but maybe there is a fix?).
An example is that we logout a user if they are not authenticated on a particular journey. However, a user may indeed be authenticated but the value is undefined so considered falsy.
5 Replies
foreign-sapphire•4y ago
Maybe you could use
isLoading provided by react query to distinguish case when there's no data vs data is not fetched yet?quickest-silver•4y ago
I'd just change the condition to:
to exclude the
undefined valuesensitive-blueOP•4y ago
Yes that is what I was thinking. I will do some testing tomorrow morning. Does your suggestion @TkDodo 🔮 take into consideration the useEffect?
quickest-silver•4y ago
not sure what you mean. first, the effect will run with
undefined and "do nothing", then, it will run again with false and redirect.
note that your component will still render with isAuthenticated: false, so maybe the better idea is to do a a <Redirect> via a component. Given that you're using react-router:
sensitive-blueOP•4y ago
Works a charm. Thanks @TkDodo 🔮 🤩