【发布时间】:2025-12-22 12:35:10
【问题描述】:
我的代码如下所示:
const countersSelector = () => (store) => store.counters;
const MethodOne = () => {
// Method: Object destructuring
const counters = useSelector(countersSelector);
console.log("render MethodOne");
useEffect(() => {
console.log("in useeffect methodone");
}, [counters]);
return (
<div>
<p>{`firstCounter: ${counters}`}</p>
<p>{`renders: ${renderCount}`}</p>
</div>
);
};
export default MethodOne;
这是完整的应用程序: https://codesandbox.io/s/redux-useselector-object-destructuring-example-forked-1m87x
并且每次counters 对象被更改(不是它的值,而是对象引用本身),useEffect() 都会被触发。
我可以以某种方式记住它,这样counters 只会在store.counters 的实际值改变时才会改变?
【问题讨论】:
标签: redux css-selectors redux-toolkit