componentwillreceiveprops react code example
Example 1: componentwillreceiveprops hooks
SomeComponent (props) => {
useEffect( () => {
console.log('someProp updated');
}, [props.someProp])
return Hi {props.someProp}
}
Example 2: shouldcomponentupdate
shouldComponentUpdate(nextProps, nextState) {
return true;
}
Example 3: componentwillreceiveprops hooks
const { useState, useEffect, useMemo } = React;
function App() {
const [count, setCount] = useState(50);
useEffect(() => {
setTimeout(() => {
setCount(150);
}, 2000);
}, []);
return ;
}
function DisplayCount(props) {
const count = useMemo(() => props.count > 100 ? 100 : props.count, [props.count]);
return {count}
;
}
ReactDOM.render(, document.getElementById("root"));