The react hook equivalent to the old componentWillReceive props can be done using the useEffect hook, just specifying the prop that we want to listen for changes in the dependency array.
I.e:
export default (props) => {
useEffect( () => {
console.log('counter updated');
}, [props.counter])
return <div>Hi {props.counter}</div>
}
For componentDidUpdate just by omitting the dependency array, the useEffect function will be called after every re-render.
I.e:
export default (props) => {
useEffect( () => {
console.log('counter updated');
})
return <div>Hi {props.counter}</div>
}
Answer from fgonzalez on Stack OverflowcomponentWillReceiveProps, componentDidUpdate for React Hook
why should I not use use state and useEffect on next?
getStaticProps or useEffect | for fetching data from the API
Useeffect and serversideprops
Videos
The react hook equivalent to the old componentWillReceive props can be done using the useEffect hook, just specifying the prop that we want to listen for changes in the dependency array.
I.e:
export default (props) => {
useEffect( () => {
console.log('counter updated');
}, [props.counter])
return <div>Hi {props.counter}</div>
}
For componentDidUpdate just by omitting the dependency array, the useEffect function will be called after every re-render.
I.e:
export default (props) => {
useEffect( () => {
console.log('counter updated');
})
return <div>Hi {props.counter}</div>
}
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]);
};
So I was building a website in the nextjs. and I want to fetch some data from the API. but now I'm confused that should I use the useEffect hook in the component itself or use the getStaticProps on the page!!!
using the useEffect is easy we have the current component access and all but,
there are so many questions while using getStaticProps like:
-
Can I do error handling in it?
-
Can I show page loading?
-
If I don't use it and use useEffect instead, will it be good?
-
Can I store data in redux store from it?
-
There could be more questions to that depending on the conditions...
I mean I can run that and test them and I know that if I pass the data to the component from the getStaticProps, it will work, or at least it should. but again will there be a right way to do those things.
So if anyone can help it would be very kind 😇
THANX
If I use useeffect hook in a next js application, do I still need serversideprops?
You can write a custom hook to provide you a previous props using useRef
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
and then use it in useEffect
const Component = (props) => {
const {receiveAmount, sendAmount } = props
const prevAmount = usePrevious({receiveAmount, sendAmount});
useEffect(() => {
if(prevAmount.receiveAmount !== receiveAmount) {
// process here
}
if(prevAmount.sendAmount !== sendAmount) {
// process here
}
}, [receiveAmount, sendAmount])
}
However its clearer and probably better and clearer to read and understand if you use two useEffect separately for each change if you want to process them separately
In case anybody is looking for a TypeScript version of usePrevious:
In a .tsx or .ts module:
import { useEffect, useRef } from "react";
const usePrevious = <T>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
};