[英]How to use one useMemo using react?
i want to use only one useMemo instead of two useMemo in one component.我只想在一个组件中使用一个 useMemo 而不是两个 useMemo。
below is my code,下面是我的代码,
const App = () => {
const isChecked = React.useMemo(() =>
checked === 'something', [checked]);
const variables = React.useMemo(() => ({
variable1,
variable2: 200
}), [variable1]);
return (
//some jsx
);
}
now the question is how can i combine the above two useMemo into one.现在的问题是如何将上述两个 useMemo 合二为一。 could someone help me with this.
有人可以帮我解决这个问题。 thanks.
谢谢。
You can use the array destructuration like this:您可以像这样使用数组解构:
const [isChecked, variables] = React.useMemo(() => [
checked === 'something',
{ variable1, variable2: 200 }],
[checked, variable1]);
However, it is less optimized, since if either checked or variable1 change, it will re execute the useMemo function for both of the variables isChecked and variables但是,它的优化程度较低,因为如果选中或 variable1 更改,它将重新执行 useMemo function 变量 isChecked 和 variables
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.