We have a ref to store the boolean value, but what if we write it this way :
export function useBoolean(initialState: boolean): [boolean, IUseBooleanCallbacks] {
  const [value, setValue] = React.useState(initialState);
  const setTrue = useConst(() => () => {
    setValue(true);
  });
  const setFalse = useConst(() => () => {
    setValue(false);
  });
  const toggle = useConst(() => () => {
    setValue((value) => !value)
  });
  return [value, { setTrue, setFalse, toggle }];
}
It ensures toggle has a constant reference, and without having to store boolean value in a ref.
@FuJuntao Thanks for filing this. Is this something you'd be willing to contribute?
@tomi-msft - Coordinate with @FuJuntao if this is something they want to spin up a PR for.
@JustSlone / @dzearing FYI
This is a simple improvement, feel free to submit a PR.
Minor thought, but just my 2c. I would love if the toggle function output took an optional argument to force it to a value. Call toggle with no arguments to toggle, or toggle(true) to force it to a state. That way we don't have to fumble with multiple functions.
Thank you all for the replies, a PR is coming right away
I have submitted the PR (#15607), but it seems that I'm not allowed to link it with this issue.
Anyway, it's ready for some reviews 馃挅
:tada:This issue was addressed in #15607, which has now been successfully released as @fluentui/[email protected].:tada:
Handy links:
Most helpful comment
I have submitted the PR (#15607), but it seems that I'm not allowed to link it with this issue.
Anyway, it's ready for some reviews 馃挅