-
Notifications
You must be signed in to change notification settings - Fork 570
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
Antonio Russo
committed
Oct 9, 2020
1 parent
683d4f0
commit dac3e6b
Showing
15 changed files
with
183 additions
and
3 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
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
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
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,72 @@ | ||
# useRenderInfo | ||
|
||
Takes a component name and prints information on how many time the component renders, at what time and how many seconds | ||
has passed since the last render. | ||
|
||
### Why? 💡 | ||
|
||
- Easily display information on components render | ||
|
||
### Basic Usage: | ||
|
||
```jsx harmony | ||
import useInterval from 'beautiful-react-hooks/useInterval'; | ||
import useRenderInfo from 'beautiful-react-hooks/useRenderInfo'; | ||
|
||
|
||
const RenderInfo = () => { | ||
const [seconds, setSeconds] = React.useState(0); | ||
|
||
// repeat the function each 1000ms | ||
useInterval(() => { | ||
setSeconds(1 + seconds); | ||
}, 1000); | ||
|
||
|
||
useRenderInfo('Module'); | ||
|
||
return ( | ||
<DisplayDemo> | ||
<p>Check the console!</p> | ||
</DisplayDemo> | ||
); | ||
}; | ||
|
||
<RenderInfo /> | ||
``` | ||
|
||
### Custom logs: | ||
|
||
```jsx harmony | ||
import useInterval from 'beautiful-react-hooks/useInterval'; | ||
import useRenderInfo from 'beautiful-react-hooks/useRenderInfo'; | ||
|
||
|
||
const RenderInfo = () => { | ||
const [seconds, setSeconds] = React.useState(0); | ||
const info = useRenderInfo(); | ||
|
||
// repeat the function each 1000ms | ||
useInterval(() => { | ||
setSeconds(1 + seconds); | ||
}, 1000); | ||
|
||
return ( | ||
<DisplayDemo> | ||
<p>I'm not using the console, {info.sinceLast} seconds passed from the last render!</p> | ||
</DisplayDemo> | ||
); | ||
}; | ||
<RenderInfo /> | ||
``` | ||
### Mastering the hook | ||
#### ✅ When to use | ||
- When debugging a component | ||
#### 🛑 What not to do | ||
- In production build, you don't want useless logs in console :) |
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
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,36 @@ | ||
import { useRef } from 'react'; | ||
|
||
const getInitial = (module) => ({ | ||
module, | ||
renders: 0, | ||
timestamp: null, | ||
sinceLast: null, | ||
}); | ||
|
||
/** | ||
* useRenderInfo | ||
* @param module | ||
* @param log | ||
* @returns {{renders: number, module: *, timestamp: null}} | ||
*/ | ||
const useRenderInfo = (module = 'Unknown component', log = true) => { | ||
const { current: info } = useRef(getInitial(module)); | ||
const now = +Date.now(); | ||
|
||
info.renders += 1; | ||
info.sinceLast = info.timestamp ? (now - info.timestamp) / 1000 : '[now]'; | ||
info.timestamp = now; | ||
|
||
if (log) { | ||
/* eslint-disable no-console */ | ||
console.group(`${module} info`); | ||
console.log(`Render no: ${info.renders}${info.renders > 1 ? `, ${info.sinceLast}s since last render` : ''}`); | ||
console.dir(info); | ||
console.groupEnd(); | ||
/* eslint-enable no-console */ | ||
} | ||
|
||
return info; | ||
}; | ||
|
||
export default useRenderInfo; |
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,45 @@ | ||
import React from 'react'; | ||
import { cleanup, renderHook } from '@testing-library/react-hooks'; | ||
import useRenderInfo from '../dist/useRenderInfo'; | ||
|
||
describe('useRenderInfo', () => { | ||
beforeEach(cleanup); | ||
|
||
afterEach(sinon.restore); | ||
|
||
it('should be a function', () => { | ||
expect(useRenderInfo).to.be.a('function'); | ||
}); | ||
|
||
it('should return an information object', () => { | ||
const name = 'Foo'; | ||
const { result: { current: info } } = renderHook(() => useRenderInfo(name, false)); | ||
|
||
expect(info).to.be.an('object'); | ||
expect(info.module).to.equal(name); | ||
expect(info.renders).to.be.a('number'); | ||
expect(info.sinceLast).to.be.a('string'); | ||
expect(info.timestamp).to.be.a('number'); | ||
}); | ||
|
||
it('should print consistent information', () => { | ||
const { result: { current: info }, rerender } = renderHook(() => useRenderInfo('foo', false)); | ||
|
||
rerender(); | ||
rerender(); | ||
|
||
expect(info.renders).to.equal(3); | ||
}); | ||
|
||
it('should print renders information in group', () => { | ||
const groupSpy = sinon.spy(console, 'group'); | ||
const groupEndSpy = sinon.spy(console, 'groupEnd'); | ||
const logSpy = sinon.spy(console, 'log'); | ||
|
||
renderHook(() => useRenderInfo(name, true)); | ||
|
||
expect(logSpy.called).to.be.true; | ||
expect(groupSpy.called).to.be.true; | ||
expect(groupEndSpy.called).to.be.true; | ||
}); | ||
}); |