-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
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. |
FWIW the bug is also reproducible on the 3.x branch: https://codesandbox.io/s/musing-blackwell-rpqhxr |
Are there any workarounds for this? |
I think a workaround is to selectively enable strict mode in other places in your app, except for Uppy. See facebook/react#16362 |
I rewrote a hook which follows a recommended pattern from this discussion reactwg/react-18#18 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;
}; |
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. |
Initial checklist
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
.The text was updated successfully, but these errors were encountered: