Skip to content

Commit

Permalink
feat: implement useLifecycleEffect hook (#652)
Browse files Browse the repository at this point in the history
* 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
JoshuaStewartEntelect authored Apr 25, 2022
1 parent af5de33 commit 357b018
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Coming from `react-use`? Check out our
— Run effect only when component unmounted.
- [**`useUpdateEffect`**](https://react-hookz.github.io/web/?path=/docs/lifecycle-useupdateeffect--example)
— Effect hook that ignores the first render (not invoked on mount).
- [**`useLifecycleLogger`**](https://react-hookz.github.io/web/?path=/docs/lifecycle-uselifecycleLogger--example)
— This hook provides a console log when the component mounts, updates and unmounts.

- #### State

Expand Down
2 changes: 1 addition & 1 deletion src/__docs__/migrating-from-react-use.story.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ Not implemented yet

#### useLogger

Not implemented yet
Implemented as [useLifecycleLogger](/docs/lifecycle-useLifecycleLogger--example)

#### useMount

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { useRerender } from './useRerender/useRerender';
export { useThrottledEffect } from './useThrottledEffect/useThrottledEffect';
export { useUnmountEffect } from './useUnmountEffect/useUnmountEffect';
export { useUpdateEffect } from './useUpdateEffect/useUpdateEffect';
export { useLifecycleLogger } from './useLifecycleLogger/useLifecycleLogger';

// State
export { useDebouncedState } from './useDebouncedState/useDebouncedState';
Expand Down
21 changes: 21 additions & 0 deletions src/useLifecycleLogger/__docs__/example.stories.tsx
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>
);
};
30 changes: 30 additions & 0 deletions src/useLifecycleLogger/__docs__/story.mdx
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`.
29 changes: 29 additions & 0 deletions src/useLifecycleLogger/__tests__/dom.ts
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', {});
});
});
12 changes: 12 additions & 0 deletions src/useLifecycleLogger/__tests__/ssr.ts
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();
});
});
19 changes: 19 additions & 0 deletions src/useLifecycleLogger/useLifecycleLogger.ts
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);
}

0 comments on commit 357b018

Please sign in to comment.