-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
useLifecycleEffect
hook (#652)
* Adding useLogger and useEffectOnce, components, test and storybook. * Adding documentation * Removing the useEffectOnce hook as it is an alias. Fixing Linting issues. * Adding dependencies array, updating documentation * Changing useLogger to useLifecycleLogger * Updating docs for useLifecycleEffect * Fixing for comments.
- Loading branch information
1 parent
af5de33
commit 357b018
Showing
8 changed files
with
115 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as React from 'react'; | ||
import { useRerender } from '../..'; | ||
import { useLifecycleLogger } from '../useLifecycleLogger'; | ||
|
||
export const Example: React.FC = () => { | ||
const rerender = useRerender(); | ||
const dependency = 'test'; | ||
useLifecycleLogger('Demo', [dependency]); | ||
|
||
return ( | ||
<div> | ||
<div>Check your console for useLifecycleLogger logs</div> | ||
<button | ||
onClick={() => { | ||
rerender(); | ||
}}> | ||
Rerender component | ||
</button> | ||
</div> | ||
); | ||
}; |
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,30 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="Lifecycle/useLifecycleLogger" component={Example} /> | ||
|
||
# useLifecycleLogger | ||
|
||
React lifecycle hook that console logs parameters as component transitions through lifecycles. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
function useLifecycleLogger(componentName: string, deps: []): void; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> | ||
|
||
#### Arguments | ||
|
||
- _**componentName**_ _`ComponentName`_ - String value to log the components name when logging the lifecycle. | ||
- **deps** _`DependencyList`_ - Dependencies list that will be passed to underlying `useEffect`. |
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 @@ | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useLifecycleLogger } from '../useLifecycleLogger'; | ||
|
||
describe('useLifecycleLogger', () => { | ||
const originalLog = console.log; // save original console.log function | ||
beforeEach(() => { | ||
console.log = jest.fn(); // create a new mock function for each test | ||
}); | ||
afterAll(() => { | ||
console.log = originalLog; // restore original console.log after all tests | ||
}); | ||
it('should return TestComponent mounted on first render', () => { | ||
const dep = 'test'; | ||
renderHook(() => useLifecycleLogger('TestComponent', [dep])); | ||
expect(console.log).toBeCalledWith('TestComponent mounted', { '0': 'test' }); | ||
}); | ||
|
||
it('should return `TestComponent updated` on second and next renders', () => { | ||
const { rerender } = renderHook(() => useLifecycleLogger('TestComponent')); | ||
|
||
expect(console.log).toBeCalledWith('TestComponent mounted', {}); | ||
|
||
rerender(); | ||
expect(console.log).toBeCalledWith('TestComponent updated', {}); | ||
|
||
rerender(); | ||
expect(console.log).toBeCalledWith('TestComponent updated', {}); | ||
}); | ||
}); |
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,12 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useLifecycleLogger } from '../useLifecycleLogger'; | ||
|
||
describe('useLifecycleLogger', () => { | ||
it('should be defined', () => { | ||
expect(useLifecycleLogger).toBeDefined(); | ||
}); | ||
it('should render', () => { | ||
const { result } = renderHook(() => useLifecycleLogger('TestComponent', [])); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
}); |
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 @@ | ||
import { DependencyList } from 'react'; | ||
import { useUpdateEffect, useMountEffect } from '..'; | ||
|
||
/** | ||
* This hook provides a console log when the component mounts, updates and unmounts. | ||
* | ||
* @param componentName Provides the name of the component in which the life cycle is being logged | ||
* @param deps Dependencies list, as for `useEffect` hook | ||
*/ | ||
export function useLifecycleLogger(componentName: string, deps?: DependencyList): void { | ||
useMountEffect(() => { | ||
console.log(`${componentName} mounted`, { ...deps }); | ||
return () => console.log(`${componentName} unmounted`); | ||
}); | ||
|
||
useUpdateEffect(() => { | ||
console.log(`${componentName} updated`, { ...deps }); | ||
}, deps); | ||
} |