Skip to content

Commit

Permalink
🏷️ [react-hooks] Introduce initializer function to useToggle (#2734)
Browse files Browse the repository at this point in the history
* [react-hooks] Introduce initializer function to useToggle

* Updates react-hooks' useToggle documentation

* Add changeset to react-hooks
  • Loading branch information
emileber committed Mar 6, 2024
1 parent 5ad9532 commit a4eae7d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-hornets-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/react-hooks': minor
---

Introduce initializer function to useToggle
2 changes: 1 addition & 1 deletion packages/react-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function Score({value}) {

### `useToggle()`

This hook provides an object that contains a boolean state value and a set of memoised callbacks to toggle it, force it to true, and force it to false. It accepts one argument that is the initial value of the state. This is useful for toggling the active state of modals and popovers.
This hook provides an object that contains a boolean state value and a set of memoised callbacks to toggle it, force it to true, and force it to false. It accepts one argument that is the initial value of the state or an initializer function returning the initial value. This is useful for toggling the active state of modals and popovers.

```tsx
function MyComponent() {
Expand Down
16 changes: 15 additions & 1 deletion packages/react-hooks/src/hooks/tests/toggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {mount} from '@shopify/react-testing';
import {useToggle} from '../toggle';

describe('useToggle', () => {
function MockComponent({initialValueIsTrue = false}) {
function MockComponent({
initialValueIsTrue = false,
}: {
initialValueIsTrue?: boolean | (() => boolean);
}) {
const {value, toggle, setTrue, setFalse} = useToggle(initialValueIsTrue);

const activeText = value ? 'true' : 'false';
Expand All @@ -25,6 +29,16 @@ describe('useToggle', () => {

const wrapperInitallyTrue = mount(<MockComponent initialValueIsTrue />);
expect(wrapperInitallyTrue).toContainReactText('Value: true');

const wrapperInitallyFunctionFalse = mount(
<MockComponent initialValueIsTrue={() => false} />,
);
expect(wrapperInitallyFunctionFalse).toContainReactText('Value: false');

const wrapperInitallyFunctionTrue = mount(
<MockComponent initialValueIsTrue={() => true} />,
);
expect(wrapperInitallyFunctionTrue).toContainReactText('Value: true');
});

it('toggles the value when the toggle callback is triggered', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/src/hooks/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useState, useCallback} from 'react';
* Returns a stateful value, and a set of memoized functions to toggle it,
* set it to true and set it to false
*/
export function useToggle(initialState: boolean) {
export function useToggle(initialState: boolean | (() => boolean)) {
const [value, setState] = useState(initialState);

return {
Expand Down

0 comments on commit a4eae7d

Please sign in to comment.