componentWillReceiveProps, componentDidUpdate for React Hook

后端 未结 8 1204
闹比i
闹比i 2020-12-25 11:00

I run into two challenges:

  • Even if, as per React guideline, derived state is discouraged, but some edge cases still need it.
    In terms of
8条回答
  •  误落风尘
    2020-12-25 11:30

    If you use the useMemo hook on top of your component and have it dependent on all your props, it runs before everything everytime props change. useEffect is triggered after the updated render and since dependent on all props it triggers after a rerender depending on all props.

    const Component = (...props) => {
       // useState, useReducer if have
       useMemo(() => {
         // componentWillReceiveProps
       },[...props]);
       // ...other logic and stuff
       useEffect(() => {
         // componentDidUpdate
       }, [...props]);
    };
    

提交回复
热议问题