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

Custom hook and strange warnings #16526

Closed
brunoscopelliti opened this issue Aug 21, 2019 · 4 comments
Closed

Custom hook and strange warnings #16526

brunoscopelliti opened this issue Aug 21, 2019 · 4 comments

Comments

@brunoscopelliti
Copy link

I've a question about a behaviour that appears strange to me.

I am writing a custom hook.

const useToggle =
  (initialState) => {
    const [isTrue, setState] = useState(initialState || false);

    const False =
      () => {
        setState(false);
      };

    const True =
      () => {
        setState(true);
      };

    return [isTrue, True, False];
  };

It works fine.

But if I try to rewrite False, and True using setState.bind I get the following warning in console.

const False = setState.bind(null, false);

const True = setState.bind(null, true);

The warning:

Warning: State updates from the useState() and useReducer() Hooks don't support the second callback argument.
To execute a side effect after rendering, declare it in the component body with useEffect().

Is this the expected behaviour?

Why is that? The two snippet appear equivalent to me.

@gaearon
Copy link
Collaborator

gaearon commented Aug 21, 2019

I'm guessing you're doing something like onClick={True} which results in onClick={e => True(e)} or onClick={r => setState(true, e)}. React thinks you're trying to pass a second argument to setState and warns it's not supported. Maybe we could scope down the warning for just the case when you pass a function.

@brunoscopelliti
Copy link
Author

Oh, I see.
So there's nothing particularly wrong about my custom hook.
@gaearon Thank you for the super fast answer.

Maybe we could scope down the warning for just the case when you pass a function.

It makes sense to me. If you think is a good idea, I can help with a PR in the coming days. Just let me know.

@gaearon
Copy link
Collaborator

gaearon commented Aug 21, 2019

Sure, a PR sounds fine.

@threepointone
Copy link
Contributor

Closing since the fix landed. Thanks @brunoscopelliti!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants