-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useAfterFirstRender): hook is currently behaving the opposite tha…
…n its purpose (#2168)
- Loading branch information
1 parent
6491d27
commit 483bf9b
Showing
9 changed files
with
88 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/core/src/hooks/useAfterFirstRender/__stories__/useAfterFirstRender.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 39 additions & 7 deletions
46
packages/core/src/hooks/useAfterFirstRender/__stories__/useAfterFirstRender.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,47 @@ | ||
import React from "react"; | ||
import React, { useState } from "react"; | ||
import useAfterFirstRender from ".."; | ||
import Button from "../../../components/Button/Button"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import Flex from "../../../components/Flex/Flex"; | ||
import Heading from "../../../components/Heading/Heading"; | ||
|
||
type Story = StoryObj<typeof useAfterFirstRender>; | ||
|
||
export default { | ||
title: "Hooks/useAfterFirstRender" | ||
}; | ||
} satisfies Meta<typeof useAfterFirstRender>; | ||
|
||
export const Overview = { | ||
export const Overview: Story = { | ||
render: () => { | ||
const isRendered = useAfterFirstRender(); | ||
return <div className="hooks-reference-element">{isRendered ? "Rendered!" : "Not yet!"}</div>; | ||
}, | ||
const isAfterFirstRender = useAfterFirstRender(); | ||
const [renderCount, setRenderCount] = useState(0); | ||
|
||
name: "Overview" | ||
const handleRerender = () => { | ||
setRenderCount(prevCount => prevCount + 1); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Heading type={Heading.types.H3} weight={Heading.weights.NORMAL}> | ||
{isAfterFirstRender.current ? "It is after the first render!" : "This is the first render!"} | ||
</Heading> | ||
<p>Rerender count: {renderCount}</p> | ||
<Button onClick={handleRerender}>Rerender component</Button> | ||
</> | ||
); | ||
}, | ||
decorators: [ | ||
Story => ( | ||
<Flex direction={Flex.directions.COLUMN} align={Flex.align.START}> | ||
<Story /> | ||
</Flex> | ||
) | ||
], | ||
parameters: { | ||
docs: { | ||
liveEdit: { | ||
scope: { Heading } | ||
} | ||
} | ||
} | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/core/src/hooks/useAfterFirstRender/__tests__/useAfterFirstRender.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import useAfterFirstRender from "../"; | ||
|
||
describe("useAfterFirstRender", () => { | ||
it("should initially set isAfterFirstRender to false", () => { | ||
const { result } = renderHook(() => useAfterFirstRender()); | ||
expect(result.current.current).toBeFalsy(); | ||
}); | ||
|
||
describe("when re-renders", () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("should set isAfterFirstRender to true after a delay", async () => { | ||
const { result } = renderHook(() => useAfterFirstRender()); | ||
jest.advanceTimersByTime(1); | ||
expect(result.current.current).toBeTruthy(); | ||
}); | ||
|
||
it("should maintain isAfterFirstRender as true on subsequent renders", () => { | ||
const { result, rerender } = renderHook(() => useAfterFirstRender()); | ||
|
||
jest.advanceTimersByTime(1); | ||
expect(result.current.current).toBeTruthy(); | ||
|
||
rerender(); | ||
expect(result.current.current).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { RefObject, useEffect, useRef } from "react"; | ||
|
||
export default function useAfterFirstRender(): RefObject<boolean> { | ||
const isAfterFirstRender = useRef(true); | ||
const isAfterFirstRender = useRef(false); | ||
|
||
useEffect(() => { | ||
window.requestAnimationFrame(() => { | ||
isAfterFirstRender.current = false; | ||
}); | ||
const timer = setTimeout(() => { | ||
isAfterFirstRender.current = true; | ||
}, 0); | ||
return () => clearTimeout(timer); | ||
}, []); | ||
|
||
return isAfterFirstRender; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters