1

There is a button that when click it toggles a value. Based on this value, the button has an icon or another icon.

Here is the code:

export const useToggle = (initialState = false) => {
  const [state, setState] = useState(initialState);
  const toggle = useCallback(() => setState((state) => !state), []);
  return [state, toggle];
};


export function Sidebar({ isOpenedInitial }: SidebarProps) {
  const [isOpened, setSidebarState] = useToggle(isOpenedInitial);

  ...
  return (
    <div>
      <button onClick={setSidebarState}>{isOpened ? <OneIcon /> : <AnotherIcon />}</button>
  ...
  )
}

It works but it has a red line under onClick word which says the following:

Type 'boolean | (() => void)' is not assignable to type 'MouseEventHandler | undefined'. Type 'false' is not assignable to type 'MouseEventHandler | undefined'

I've googled this message, found a question about it here where the accepted answer states to change from onClick={setSidebarState} to onClick={() => setSidebarState}.

The change was made, there is no warning anymore but the button doesn't work now. I click on it and nothing happens.

Any ideas?

6
  • in your onClick, you're missing brackets: onClick={() => setSidebarState()} Commented Nov 11, 2021 at 13:41
  • @PradipDhakal Ive tried like that and it says: This expression is not callable. Not all constituents of type 'boolean | (() => void)' are callable. Type 'false' has no call signatures. Commented Nov 11, 2021 at 13:42
  • are you using typescript? Commented Nov 11, 2021 at 13:43
  • He is using typescript Commented Nov 11, 2021 at 13:45
  • 1
    Then @jkaczmarkiewicz solution should work for you. Commented Nov 11, 2021 at 13:46

2 Answers 2

2

If you mark return value of useToggle as const it will infer correct type of second element of array

export const useToggle = (initialState = false) => {
  const [state, setState] = useState(initialState);
  const toggle = useCallback(() => setState((state) => !state), []);
  return [state, toggle] as const;
};

export function Sidebar() {
  const [isOpened, setSidebarState] = useToggle();

  return (
    <div>
      <button onClick={setSidebarState}>{isOpened ? ... : ...}</button>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Now, you need to explicit say that setSideBarState is a function, so you need to put setSideBarState()

5 Comments

you mean onClick={setSidebarState()} ?
Nope, onClick={() => setSidebarState()}, using arrow function.
I've tried like that and has this error message: This expression is not callable. Not all constituents of type 'boolean | (() => void)' are callable. Type 'false' has no call signatures.
🤔, wait, I'm looking for.
You need to use "useToggle" or just wanting to learn about it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.