vazco / uniforms

A React library for building forms from any schema.
https://uniforms.tools
MIT License
1.95k stars 240 forks source link

componentWillUnmount causing setState to be set to () => {} in Next.js #1316

Closed cohenrobinson closed 5 months ago

cohenrobinson commented 5 months ago

Hello!

I just migrated from using the standard CRA installation to now using React + Next.js. Unsure if the following is a bug with Next.js (14.1.4) or uniforms (4.0.0-alpha.5)

After migrating, all uniforms AutoForms I implemented would now not have the AutoField state updated, even though they were previously working.

I implemented a very straightforward AutoForm per the docs just to see if it was an issue with my custom implementation, but that too failed.

After a bit of digging, the componentWillUnmount was causing the setState to be set to () => {}, despite the component being mounted subsequently.

I removed the this.setState = () => { }; line and was able to verify this now works as expected and the field state is being updated.

componentWillUnmount() {
        this.mounted = false;
        if (this.delayId) {
            clearTimeout(this.delayId);
        }
        // There are at least 4 places where we'd need to check, whether or not we
        // actually perform `setState` after the component gets unmounted. Instead,
        // we override it to hide the React warning. Also because React no longer
        // will raise it in the newer versions.
        // https://github.com/facebook/react/pull/22114
        // https://github.com/vazco/uniforms/issues/1152
        this.setState = () => { };
    }

I suspect this must be with the way Next.js deals with state and mounting of components. However, I can't find anything to suggest it's the Next.js installation that's causing the issue (not directly at least).

kestarumper commented 5 months ago

Hi @cohenrobinson, there is a similar issue https://github.com/vazco/uniforms/issues/1194#issuecomment-1784641342. Could you have a look at it and tell me if it helps in any way? Try disabling strict mode.

kestarumper commented 5 months ago

Related: #1324

kestarumper commented 5 months ago

Closing due to no response. It will be fixed in #1324.