-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
Describe the bug
I am writing a React+react-query application and I am running into a problem when I switched from development mode to production mode. Production mode is a mix of running npm run build and serving those HTML/JavaScript/CSS files inside a server (maybe using npx serve dist). While development mode is just running npm run dev (or npm start if using CRA).
const useMyQuery = () =>
useQuery(
["myAwesomeData"],
async () => {
const { data } = await axios.get(`http://http.us/500`);
return data;
},
{ placeholderData: { myAwesomeProperty: [] } }
);
const App = () => {
const { isError } = useMyQuery();
return isError ? <div>Error</div> : <Child />;
};
const Child = () => {
const { data } = useMyQuery();
return (
<pre>
<code>{JSON.stringify(data.myAwesomeProperty)}</code>
</pre>
);
};For the sake of simplicity, I omitted QueryClientProvider and its client. Also placeholderData is being used as a loading state, which allows Child to be properly rendered while real data is loading.
The most important part here is data.myAwesomeProperty AFTER the loading state - which throws an error ONLY when running in production. The error produced is:
TypeError: Cannot read property 'myAwesomeProperty' of undefined
When running on development, the <div>Error</div> appears as expected.
My main concern here is NOT solve the problem (as I could simply add a if statement on Child component to prevent accessing data.myAwesomeProperty).
Instead, I would like to know why there is a difference between development's and production's behavior? Is it documented somewhere?
To Reproduce
Steps to reproduce the behavior:
- https://codesandbox.io/s/react-query-rerender-6pvhy
- Run to see the Error word
- Deploy to any option available and see the expcetion on console terminal.
- You can also see it deployed here: https://csb-6pvhy.netlify.app/
OR
- Clone the repo: https://github.com/richardaum/error-sandbox
- Run
npm run devto run development mode. See the "Error" word appearing. - Run
npm run buildand serve the html usingnpx serve dist. See the exception on console terminal.
Expected behavior
The "Error" word should also appear on production mode and shouldn't be any differences.
Desktop
- react: 17.0.2
- react-query: 3.21.1
- OS: Windows 10
- Browser: Chrome
- Version: 93.0.4577.63