facebook / react

The library for web and native user interfaces.
https://react.dev
MIT License
228.45k stars 46.74k forks source link

Warn when setState is a function that doesn't return #13111

Closed swyxio closed 4 years ago

swyxio commented 6 years ago

Do you want to request a feature or report a bug?

I am proposing adding a warning in development.

What is the current behavior?

when I do this.setState(({ bool }) => { bool: !bool }); this is valid javascript but is meaningless in React. ESLint:no-label helps to catch this but we can probably do one better by building a warning into dev-mode React itself.

sandbox demo: https://codesandbox.io/s/xopj5nx07o

What is the expected behavior?

warn when a function is applied to setState that returns undefined. if the user wants to indicate nothing changed they should return null.

gaearon commented 6 years ago

I wish we did this earlier. Might be too late now because there’s probably a lot of code using this pattern. #strictmodeonly?

tsiq-swyx commented 6 years ago

strictmodeonly works for me.

i'm not sure i understand, why would there be a lot of code using this pattern? accidental no-op is far more likely than intentional no-op?

also we'd just be doing a warning, i reckon if people think they know what they're doing we can let them ignore the warning. i dunno, is that inline with react's warning philosophy?

alexkrolick commented 6 years ago

i'm not sure i understand, why would there be a lot of code using this pattern? accidental no-op is far more likely than intentional no-op?

Probably something like:

this.setState(state => { if (condition) return { foo: !state.foo } }) // no else
gaearon commented 6 years ago

i'm not sure i understand, why would there be a lot of code using this pattern? accidental no-op is far more likely than intentional no-op?

I don't think it's necessarily more likely.

also we'd just be doing a warning, i reckon if people think they know what they're doing we can let them ignore the warning.

No, if we add a warning, we don't want people to ignore it. This devalues warnings.

swyxio commented 6 years ago

strict mode it is

aweary commented 6 years ago

@sw-yx do you want to submit a PR for this?

tsiq-swyx commented 6 years ago

yup i will attempt it this weekend if thats ok?

On Tue, Jun 26, 2018 at 10:48 PM Brandon Dail notifications@github.com wrote:

@sw-yx https://github.com/sw-yx do you want to submit a PR for this?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/facebook/react/issues/13111#issuecomment-400525534, or mute the thread https://github.com/notifications/unsubscribe-auth/AiT1gtLGCY5yqTVilBJ9MI8KAYdTweB-ks5uAvJkgaJpZM4U4csG .

swyxio commented 6 years ago

ok i had a rough day and felt like i needed the psychological pickup so i'm going to try this now. for anyone following along i am marking out the thought process.

after adding my warning:

        if (__DEV__) {
          if (StrictMode && partialState === undefined) {
            // Warn if partialState is undefined
            // dev likely forgot to return in setState
            // encourage returning null for no-op in strict mode
            warning(
              false,
              'A setState updater function was called that returned undefined. ' +
                'Check if you forgot to return a value in your setState updater ' +
                'function. If a no-op was intended, return null.'
            );
          }
        }
KiritoCheng commented 6 years ago

I use with typescript ,now its's error "Cannot assign to 'state' because it is a constant or a read-only property." I just write constructor(props: any) { super(props); this.state = { a:'a' } }

tsiq-swyx commented 6 years ago

sorry, i dont think this has anything to do with my PR. are you commenting in the right issue? also if you want help with React + Typescript please see https://github.com/sw-yx/react-typescript-cheatsheet

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

stale[bot] commented 4 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!