Skip to content

Custom hook and strange warnings  #16526

@brunoscopelliti

Description

@brunoscopelliti

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions