Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/react is not Compatible with React.StrictMode #3935

Closed
2 tasks done
rart opened this issue Aug 1, 2022 · 6 comments · Fixed by #4223
Closed
2 tasks done

@uppy/react is not Compatible with React.StrictMode #3935

rart opened this issue Aug 1, 2022 · 6 comments · Fixed by #4223
Assignees
Labels
Bug React React components or other React integration issues

Comments

@rart
Copy link
Contributor

rart commented Aug 1, 2022

Initial checklist

  • I understand this is a bug report and questions should be posted in the Community Forum
  • I searched issues and couldn’t find anything (or linked relevant results below)

Link to runnable example

https://codesandbox.io/s/nifty-fire-7luyeb

Steps to reproduce

When using React.StrictMode the Dashboard doesn't render. To reproduce, wrap your app or component containing the Dashboard in <React.StrictMode>. See codesandbox above.

Expected behavior

Dashboard — and other components — works regardless of StrictMode

Actual behavior

The Dashboard renders an empty square without most functionality.

Most of this may be due to the usage of class components in @uppy/react.

@mifi
Copy link
Contributor

mifi commented Aug 29, 2022

This might be because in dev-mode, React with StrictMode will mount every component twice to reveal any bugs related to shared state and race conditions not handled by frequent mount/unmount. We should definitely fix this, because async rendering needs this to work.

@Murderlon Murderlon added React React components or other React integration issues and removed Triage labels Aug 29, 2022
@aduh95
Copy link
Contributor

aduh95 commented Sep 9, 2022

FWIW the bug is also reproducible on the 3.x branch: https://codesandbox.io/s/musing-blackwell-rpqhxr

@d-pollard
Copy link

Are there any workarounds for this?

@mifi
Copy link
Contributor

mifi commented Sep 27, 2022

I think a workaround is to selectively enable strict mode in other places in your app, except for Uppy. See facebook/react#16362

@Stouffi
Copy link

Stouffi commented Sep 29, 2022

I rewrote a hook which follows a recommended pattern from this discussion reactwg/react-18#18
I've successfully tested it with React.StrictMode but the drawback is that it would be a breaking change as the uppy instance is no longer returned directly by the hook (and it was bad according to the new react docs, https://beta.reactjs.org/learn/referencing-values-with-refs#best-practices-for-refs, "don't read or write ref.current during rendering").

I made a codesandbox to compare the current useUppy which make the app fails at some point to retrieve the correct uppy instance and a new implementation compliant with StrictMode.

https://codesandbox.io/s/useuppy-c6688u?file=/src/Unsafe.tsx

Start by clicking Render button which will log an error that the TestPlugin is no longer defined (that is the instance has been closed)

The new hook code (written in TypeScript) would be

import Uppy from "@uppy/core";
import { useEffect, useRef } from "react";

export const useSafeUppy = (factory: () => Uppy) => {
  const ref = useRef<Uppy>();
  const getterRef = useRef(() => {
    if (ref.current === undefined) {
      ref.current = factory();
    }
    return ref.current;
  });
  useEffect(() => {
    return () => {
      if (ref.current !== undefined) {
        ref.current.close();
        ref.current = undefined;
      }
    };
  }, []);
  return getterRef;
};

@Murderlon
Copy link
Member

Thanks for investigating this! I'm inclined to introduce a breaking change if we fix strict mode and at the same time get rid of a bad practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug React React components or other React integration issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants