-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useFirstMountState & useRendersCount hooks (#769)
feat(useFirstMountState): hook to track if render is first; feat(useRendersCount): hook to track renders count; refactor(useUpdateEffect): now uses useFirstMountState hook; refactor(usePreviousDistinct): now uses with useFirstMountState hook; docs: update readme;
- Loading branch information
Showing
12 changed files
with
175 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# `useFirstMountState` | ||
|
||
Returns `true` if component is just mounted (on first render) and `false` otherwise. | ||
|
||
## Usage | ||
|
||
```typescript jsx | ||
import * as React from 'react'; | ||
import { useFirstMountState } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const isFirstMount = useFirstMountState(); | ||
const update = useUpdate(); | ||
|
||
return ( | ||
<div> | ||
<span>This component is just mounted: {isFirstMount ? 'YES' : 'NO'}</span> | ||
<br /> | ||
<button onClick={update}>re-render</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```typescript | ||
const isFirstMount: boolean = useFirstMountState(); | ||
``` |
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,29 @@ | ||
# `useRendersCount` | ||
|
||
Tracks compontent's renders count including the first render. | ||
|
||
## Usage | ||
|
||
```typescript jsx | ||
import * as React from 'react'; | ||
import { useRendersCount } from "react-use"; | ||
|
||
const Demo = () => { | ||
const update = useUpdate(); | ||
const rendersCount = useRendersCount(); | ||
|
||
return ( | ||
<div> | ||
<span>Renders count: {rendersCount}</span> | ||
<br /> | ||
<button onClick={update}>re-render</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```typescript | ||
const rendersCount: number = useRendersCount(); | ||
``` |
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,22 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useFirstMountState } from '../useFirstMountState'; | ||
import useUpdate from '../useUpdate'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const isFirstMount = useFirstMountState(); | ||
const update = useUpdate(); | ||
|
||
return ( | ||
<div> | ||
<span>This component is just mounted: {isFirstMount ? 'YES' : 'NO'}</span> | ||
<br /> | ||
<button onClick={update}>re-render</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('State|useFirstMountState', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useFirstMountState.md')} />) | ||
.add('Demo', () => <Demo />); |
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,22 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useRendersCount } from '../useRendersCount'; | ||
import useUpdate from '../useUpdate'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const update = useUpdate(); | ||
const rendersCount = useRendersCount(); | ||
|
||
return ( | ||
<div> | ||
<span>Renders count: {rendersCount}</span> | ||
<br /> | ||
<button onClick={update}>re-render</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('State|useRendersCount', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useRendersCount.md')} />) | ||
.add('Demo', () => <Demo />); |
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,13 @@ | ||
import { useRef } from 'react'; | ||
|
||
export function useFirstMountState(): boolean { | ||
const isFirst = useRef(true); | ||
|
||
if (isFirst.current) { | ||
isFirst.current = false; | ||
|
||
return true; | ||
} | ||
|
||
return isFirst.current; | ||
} |
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,5 @@ | ||
import { useRef } from 'react'; | ||
|
||
export function useRendersCount(): number { | ||
return ++useRef(0).current; | ||
} |
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,22 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useFirstMountState } from '../src'; | ||
|
||
describe('useFirstMountState', () => { | ||
it('should be defined', () => { | ||
expect(useFirstMountState).toBeDefined(); | ||
}); | ||
|
||
it('should return boolean', () => { | ||
expect(renderHook(() => useFirstMountState()).result.current).toEqual(expect.any(Boolean)); | ||
}); | ||
|
||
it('should return true on first render and false on all others', () => { | ||
const hook = renderHook(() => useFirstMountState()); | ||
|
||
expect(hook.result.current).toBe(true); | ||
hook.rerender(); | ||
expect(hook.result.current).toBe(false); | ||
hook.rerender(); | ||
expect(hook.result.current).toBe(false); | ||
}); | ||
}); |
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,22 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useRendersCount } from '../src'; | ||
|
||
describe('useRendersCount', () => { | ||
it('should be defined', () => { | ||
expect(useRendersCount).toBeDefined(); | ||
}); | ||
|
||
it('should return number', () => { | ||
expect(renderHook(() => useRendersCount()).result.current).toEqual(expect.any(Number)); | ||
}); | ||
|
||
it('should return actual number of renders', () => { | ||
const hook = renderHook(() => useRendersCount()); | ||
|
||
expect(hook.result.current).toBe(1); | ||
hook.rerender(); | ||
expect(hook.result.current).toBe(2); | ||
hook.rerender(); | ||
expect(hook.result.current).toBe(3); | ||
}); | ||
}); |