Skip to content

Bug: Different behavior about rerendering on development and production #2644

@richardaum

Description

@richardaum

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:

  1. https://codesandbox.io/s/react-query-rerender-6pvhy
  2. Run to see the Error word
  3. Deploy to any option available and see the expcetion on console terminal.
  4. You can also see it deployed here: https://csb-6pvhy.netlify.app/

OR

  1. Clone the repo: https://github.com/richardaum/error-sandbox
  2. Run npm run dev to run development mode. See the "Error" word appearing.
  3. Run npm run build and serve the html using npx 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions