项目作者: erkobridee

项目描述 :
useful react hooks to manage any function component lifecycle
高级语言: TypeScript
项目地址: git://github.com/erkobridee/react-lifecycle-hooks.git
创建时间: 2021-03-22T15:20:16Z
项目社区:https://github.com/erkobridee/react-lifecycle-hooks

开源协议:ISC License

下载


Actions Status codecov GitHub license GitHub Repo stars Storybook

react lifecycle hooks

According to A Complete Guide to useEffect | Overreacted

Keep in mind that the mental model for effects is different from componentDidMount and other lifecycles, and trying to find their exact equivalents may confuse you more than help

So, to keep peace of mind and manage any function component lifecycle, follow the equivalents hooks to the cases: useConstructor, useDidMount, useDidUpdate and useWillUnmount, plus useForceRender

👋 IMPORTANT: this library requires a peer dependency of the react.js v16.8.0 or newer

Install

  1. npm install --save @erkobridee/react-lifecycle-hooks

if you are using on a TypeScript, you’ll need to install

  1. npm install --save-dev @erkobridee/ts-definitions-common

API

useConstructor

  1. import { useConstructor } from '@erkobridee/react-lifecycle-hooks';
  2. export const Component = () => {
  3. useConstructor(() => console.log(`executes before mount the component`));
  4. return <div>Component</div>;
  5. };
  6. export default Component;

useDidMount

  1. import { useDidMount } from '@erkobridee/react-lifecycle-hooks';
  2. export const Component = () => {
  3. useDidMount(() => console.log(`executes after component mounted`));
  4. return <div>Component</div>;
  5. };
  6. export default Component;

useDidUpdate

  1. import React from 'react';
  2. import { useDidUpdate } from '@erkobridee/react-lifecycle-hooks';
  3. export const Component = () => {
  4. const [count, setCount] = React.useState(0);
  5. useDidUpdate(() => console.log(`executes whenever the component updates`));
  6. useDidUpdate(() => console.log(`count value updated to ${count}`), [count]);
  7. const resetClickHandler = () => setCount(0);
  8. const addClickHandler = () => setCount((prevValue) => prevValue + 1);
  9. const subtractClickhandler = () => setCount((prevValue) => prevValue - 1);
  10. return (
  11. <div>
  12. <div>Component</div>
  13. <div>count: {count}</div>
  14. <div>
  15. <button onClick={resetClickHandler}>reset</button>
  16. <button onClick={addClickHandler}>add</button>
  17. <button onClick={subtractClickhandler}>subtract</button>
  18. </div>
  19. </div>
  20. );
  21. };
  22. export default Component;

useWillUnmount

  1. import React from 'react';
  2. import { useWillUnmount } from '@erkobridee/react-lifecycle-hooks';
  3. const InnerComponent = () => {
  4. useWillUnmount(() => console.log(`executes before unmount the component`));
  5. return <div>Inner Component</div>;
  6. };
  7. const Component = () => {
  8. const [show, setShow] = React.useState(true);
  9. const removeClickHandler = () => setShow(false);
  10. return (
  11. <div>
  12. <div>Component</div>
  13. {show && (
  14. <div>
  15. <button onClick={removeClickHandler}>remove</button>
  16. <InnerComponent ></InnerComponent>
  17. </div>
  18. )}
  19. </div>
  20. );
  21. };
  22. export default Component;

useForceRender

  1. import React from 'react';
  2. import { useForceRender } from '@erkobridee/react-lifecycle-hooks';
  3. export const Component = () => {
  4. const forceRender = useForceRender();
  5. const forceRenderClickHandler = () => forceRender();
  6. React.useEffect(() => {
  7. console.log(`component render`);
  8. });
  9. return (
  10. <div>
  11. <div>Component</div>
  12. <div>
  13. <button click={forceRenderClickHandler}>force render</button>
  14. </div>
  15. </div>
  16. );
  17. };
  18. export default Component;

Useful references