Fluentui: Improvment for react-hook `useBoolean`

Created on 19 Oct 2020  路  6Comments  路  Source: microsoft/fluentui

https://github.com/microsoft/fluentui/blob/3346b09e82b41f6b5b96fe1a94c8d122b3a4625b/packages/react-hooks/src/useBoolean.ts#L21-L38

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.

Fluent UI react Investigation react-hooks In PR

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 馃挅

All 6 comments

@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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rickyp-ms picture rickyp-ms  路  3Comments

luisrudge picture luisrudge  路  3Comments

carruthe picture carruthe  路  3Comments

justinwilaby picture justinwilaby  路  3Comments

holysnake91 picture holysnake91  路  3Comments