Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement useRenderCount hook #990

Merged
merged 4 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ Coming from `react-use`? Check out our
Returns the most recent distinct value passed to the hook on previous render.
- [**`useRafState`**](https://react-hookz.github.io/web/?path=/docs/state-userafstate--example) —
Like `React.useState`, but state is only updated within animation frame.
- [**`useRenderCount`**](https://react-hookz.github.io/web/?path=/docs/state-userendercount--example) —
Tracks component's render count including first render.
- [**`useSafeState`**](https://react-hookz.github.io/web/?path=/docs/state-usesafestate--page) —
Like `useState`, but its state setter is guarded against sets on unmounted component.
- [**`useSet`**](https://react-hookz.github.io/web/?path=/docs/state-useset--example) — Tracks the
Expand Down
4 changes: 3 additions & 1 deletion src/__docs__/migrating-from-react-use.story.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,9 @@ No API changes.

#### useRendersCount

Not implemented yet
Implemented as [useRenderCount](/docs/state-userendercount--example)

No API changes.

#### createGlobalState

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export { useMediatedState } from './useMediatedState/useMediatedState';
export { usePrevious } from './usePrevious/usePrevious';
export { usePreviousDistinct } from './usePreviousDistinct/usePreviousDistinct';
export { useRafState } from './useRafState/useRafState';
export { useRenderCount } from './useRenderCount/useRenderCount';
export { useSafeState } from './useSafeState/useSafeState';
export { useSet } from './useSet/useSet';
export { useToggle } from './useToggle/useToggle';
Expand Down
16 changes: 16 additions & 0 deletions src/useRenderCount/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { useRerender } from '../../useRerender/useRerender';
import { useRenderCount } from '../useRenderCount';

export const Example: React.FC = () => {
const renders = useRenderCount();
const rerender = useRerender();

return (
<div>
<div>This component has rendered {renders} time(s)</div>
<br />
<button onClick={rerender}>Rerender</button>
</div>
);
};
29 changes: 29 additions & 0 deletions src/useRenderCount/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Canvas, Meta, Story} from '@storybook/addon-docs';
import {ImportPath} from '../../__docs__/ImportPath';
import {Example} from './example.stories';

<Meta title="State/useRenderCount" component={Example}/>

# useRenderCount

Tracks component's render count including the first render.

#### Example

<Canvas>
<Story story={Example} inline/>
</Canvas>

## Reference

```ts
export function useRenderCount(): number;
```

#### Importing

<ImportPath/>

#### Return

- `number` of renders performed.
16 changes: 16 additions & 0 deletions src/useRenderCount/__tests__/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { renderHook } from '@testing-library/react-hooks';
import { useRenderCount } from '../useRenderCount';

describe('useRendersCount', () => {
it('should be defined', () => {
expect(useRenderCount).toBeDefined();
});

it('should return amount of renders performed', () => {
const { result, rerender } = renderHook(useRenderCount);

expect(result.current).toBe(1);
rerender();
expect(result.current).toBe(2);
});
});
14 changes: 14 additions & 0 deletions src/useRenderCount/__tests__/ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { renderHook } from '@testing-library/react-hooks/server';
import { useRenderCount } from '../useRenderCount';

describe('useRendersCount', () => {
it('should be defined', () => {
expect(useRenderCount).toBeDefined();
});

it('should return proper amount of renders performed', () => {
const { result } = renderHook(useRenderCount);

expect(result.current).toBe(1);
});
});
10 changes: 10 additions & 0 deletions src/useRenderCount/useRenderCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useRef } from 'react';

/**
* Tracks component's render count including first render.
*/
export function useRenderCount(): number {
const rendersCount = useRef(0);

return ++rendersCount.current;
}
18 changes: 6 additions & 12 deletions src/useRerender/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import * as React from 'react';
import { useRef } from 'react';
import { useRerender } from '../..';
import { useRenderCount } from '../../useRenderCount/useRenderCount';
import { useRerender } from '../useRerender';

export const Example: React.FC = () => {
const count = useRef(0);
const renders = useRenderCount();
const rerender = useRerender();

count.current += 1;

return (
<div>
<div>This component has rendered {count.current} times</div>
<button
onClick={() => {
rerender();
}}>
Rerender
</button>
<div>This component has rendered {renders} time(s)</div>
<br />
<button onClick={rerender}>Rerender</button>
</div>
);
};