-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7e7083
commit b02c8ec
Showing
5 changed files
with
153 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
id: useWhyDidYouUpdate | ||
title: useWhyDidYouUpdate | ||
sidebar_label: useWhyDidYouUpdate | ||
--- | ||
|
||
## About | ||
|
||
A hook that can track which value change caused a rerender | ||
|
||
## Examples | ||
|
||
```tsx | ||
import {useWhyDidYouUpdate} from "rooks" | ||
export default function App() { | ||
useWhyDidYouUpdate(); | ||
return null | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import React from "react"; | ||
import { | ||
screen, | ||
render, | ||
act, | ||
fireEvent, | ||
cleanup, | ||
} from "@testing-library/react"; | ||
import { useWhyDidYouUpdate } from "@/hooks/useWhyDidYouUpdate"; | ||
import { waitFor } from "@testing-library/react"; | ||
import { useCounter } from ".."; | ||
|
||
describe("useWhyDidYouUpdate", () => { | ||
let TestComponent: React.FC<{ | ||
enableLogging?: boolean; | ||
}>; | ||
beforeEach(() => { | ||
jest.spyOn(global.console, "log").mockImplementation(jest.fn); | ||
}); | ||
beforeEach(() => { | ||
TestComponent = ({ enableLogging = true }) => { | ||
const { value, increment } = useCounter(0); | ||
useWhyDidYouUpdate("TestComponent", { value }, enableLogging); | ||
return <button onClick={increment}>Increment</button>; | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
(console.log as jest.Mock).mockRestore(); | ||
cleanup(); | ||
}); | ||
|
||
it("should not log on initial render", () => { | ||
expect.hasAssertions(); | ||
|
||
render(<TestComponent />); | ||
|
||
expect(console.log).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should log on prop updates", async () => { | ||
expect.hasAssertions(); | ||
|
||
render(<TestComponent />); | ||
|
||
const button = screen.getByRole("button"); | ||
|
||
act(() => { | ||
fireEvent( | ||
button, | ||
new MouseEvent("click", { | ||
bubbles: true, | ||
cancelable: true, | ||
}) | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(console.log).toHaveBeenCalledWith( | ||
"[why-did-you-update]", | ||
"TestComponent", | ||
{ value: { from: 0, to: 1 } } | ||
); | ||
}); | ||
}); | ||
|
||
it("should not log when enableLogging is false", () => { | ||
expect.hasAssertions(); | ||
render(<TestComponent enableLogging={false} />); | ||
const button = screen.getByRole("button"); | ||
act(() => { | ||
fireEvent( | ||
button, | ||
new MouseEvent("click", { | ||
bubbles: true, | ||
cancelable: true, | ||
}) | ||
); | ||
}); | ||
|
||
expect(console.log).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,42 @@ | ||
/** | ||
* useWhyDidYouUpdate | ||
* @description A hook that can track which value change caused a rerender | ||
* @see {@link https://rooks.vercel.app/docs/useWhyDidYouUpdate} | ||
*/ | ||
import { useEffect, useRef } from "react"; | ||
import { useDidUpdate } from "./useDidUpdate"; | ||
|
||
export type PropsRecord = Record<string, unknown>; | ||
|
||
export function useWhyDidYouUpdate( | ||
componentName: string, | ||
currentProps: PropsRecord, | ||
enableLogging = true | ||
) { | ||
const previousProps = useRef<PropsRecord>({}); | ||
useDidUpdate(() => { | ||
if (previousProps.current && enableLogging) { | ||
const combinedKeys = Object.keys({ | ||
...previousProps.current, | ||
...currentProps, | ||
}); | ||
const changedProps: PropsRecord = {}; | ||
combinedKeys.forEach((key) => { | ||
if (!Object.is(previousProps.current[key], currentProps[key])) { | ||
changedProps[key] = { | ||
from: previousProps.current[key], | ||
to: currentProps[key], | ||
}; | ||
} | ||
}); | ||
|
||
if (Object.keys(changedProps).length) { | ||
console.log("[why-did-you-update]", componentName, changedProps); | ||
} | ||
} | ||
}, [currentProps, componentName, enableLogging]); | ||
|
||
useEffect(() => { | ||
previousProps.current = currentProps; | ||
}); | ||
} |
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