update state when props change in react
to update state when props change in react:
-
pass
props
as a dependency touseeffect
the hook. -
every time
props
changes are made,useeffect
the logic in is re-run.
import {useeffect, usestate} from 'react';
functionchild({parentcount}) {
const [childcount, setchildcount] = usestate(0);
useeffect(() => {
setchildcount(parentcount * 2);
console.log('useeffect logic ran');
}, [parentcount]); // 👈️ add props as a dependency
return (
<div><button>child count {childcount}button>div>
);
}
export default functionparent() {
const [parentcount, setparentcount] = usestate(0);
return (
<div><buttononclick={() => setparentcount(current => current 1)}>
parent count: {parentcount}
button><hr /><childparentcount={parentcount} />div>
);
}
we use useeffect
hooks to update the component's state when its props change.
useeffect(() => {
setchildcount(parentcount * 2);
console.log('useeffect logic ran');
}, [parentcount]); // 👈️ add props as a dependency
useeffect
the logic in the hook is re-run every time one of its dependencies changes .
every parentcount
time the property changes, useeffect
the hook is re-run and we use setchildcount
the function to update the state.
add all the properties you want to track
useeffect
to the hook's dependencies array.
note
, the function we pass to the useeffect hook will also be called on the initial render.
if you don't want to run the logic in your hook on the initial render useeffect
, but only when a specific property changes, use a ref to return early on the initial render.
import {useeffect, useref, usestate} from 'react';
functionchild({parentcount}) {
const [childcount, setchildcount] = usestate(0);
const isfirstrender = useref(true);
useeffect(() => {
if (isfirstrender.current) {
isfirstrender.current = false;
return; // 👈️ if it is the first rendering, please return early
}
setchildcount(parentcount * 2);
console.log('useeffect logic ran');
}, [parentcount]);
return (
<div><button>child count {childcount}button>div>
);
}
export default functionparent() {
const [parentcount, setparentcount] = usestate(0);
return (
<div><buttononclick={() => setparentcount(current => current 1)}>
parent count: {parentcount}
button><hr /><childparentcount={parentcount} />div>
);
}
when running the hook on mount useeffect
, we use ref to exit early.
use this method if we want to listen to property changes but need to skip the first render.
it's important to note that if we update the value of a prop and that prop is in the hook's dependencies array, you'll end up in an infinite re-rendering loop.
below is an example that demonstrates this problem.
import {useeffect, usestate} from 'react';
functionchild({parentcount, setparentcount}) {
useeffect(() => {
// 👇️ this will cause an infinite loop
setparentcount(current => current 1);
console.log('useeffect logic ran');
}, [parentcount, setparentcount]); // 👈️ parentcount is a dependency
return (
<div><button>parent count {parentcount}button>div>
);
}
export default functionparent() {
const [parentcount, setparentcount] = usestate(0);
return (
<div><childsetparentcount={setparentcount}parentcount={parentcount} />div>
);
}
the problem is that we parentcount
added the property to the hook's dependencies array, but we also updated its value inside the hook.
every useeffect
time is run parentcount
the value of changes, which will re-run the hook again because parentcount
is in its dependencies array.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
publish date:2025/03/09 views:170 category:react
-
要在 react 状态下替换数组中的对象:使用 map() 方法遍历数组。 在每次迭代中,检查是否满足某个条件。 替换满足条件的对象并按原样返回所有其他对象。
how to call a function only once in react
publish date:2025/03/09 views:127 category:react
-
使用 useeffect 钩子在 react 中只调用一次函数。 当 useeffect 钩子传递一个空的依赖数组时,它仅在组件挂载时运行。 当我们必须在组件安装时获取数据时,这是首选方法。
run react hooks when a component unmounts
publish date:2025/03/09 views:81 category:react
-
卸载组件时,使用 useeffect 钩子运行react钩子。 我们从 useeffect 钩子返回的函数在组件卸载时被调用,可用于清理目的。
publish date:2025/03/09 views:181 category:react
-
在 react 中使用带有索引的 map() 方法:在数组上调用 map() 方法。传递给 map() 方法的函数会被元素和索引调用。
setting onclick listener on links in react
publish date:2025/03/09 views:182 category:react
-
要在 react 中的链接上设置 onclick 监听器:在链接上设置 onclick 属性。每次单击链接时,都会调用我们传递给道具的函数。
remove an object the state array in react
publish date:2025/03/09 views:72 category:react
-
在 react 中从状态数组中移除一个对象:使用 filter() 方法遍历数组 在每次迭代中,检查是否满足条件。将状态设置为 filter 方法返回的新数组。
how to create a sleep function in react.js
publish date:2025/03/09 views:191 category:react
-
在 react 中创建 sleep 函数:定义一个以毫秒数为参数的函数。该函数应返回一个 promise,该 promise 在提供的毫秒数后得到解决。
how to handle visibility: hidden in react
publish date:2025/03/09 views:62 category:react
-
在 react 中将 css visibility 属性设置为 hidden:在指示元素是否应该可见的状态中存储一个布尔值。有条件地在元素的样式属性中设置可见性属性。例如,style={{visibility: isvisible ? “visible”
publish date:2025/03/09 views:70 category:react
-
在 react 中更新对象状态数组:使用 map() 方法遍历数组。在每次迭代中,检查是否满足某个条件。更新符合条件的对象的属性。