admin管理员组文章数量:1391943
i want to use one useMemo instead of many React.useMemo blocks.
below is my code,
const App = () => {
const isChecked = React.useMemo(() => {
const source = get(data, 'somesource');
return source === 'source1' ||
source === 'source2';
}, [data]);
const details = React.useMemo(() => {
return get(data, 'details');
}, [data]);
const types = React.useMemo(() => {
return get(data, 'types', []);
}, [data]);
return (
//some jsx
);
}
How can i rewrite the above code to use one useMemo. could someone help me with this. i am new to using react. thanks.
i want to use one useMemo instead of many React.useMemo blocks.
below is my code,
const App = () => {
const isChecked = React.useMemo(() => {
const source = get(data, 'somesource');
return source === 'source1' ||
source === 'source2';
}, [data]);
const details = React.useMemo(() => {
return get(data, 'details');
}, [data]);
const types = React.useMemo(() => {
return get(data, 'types', []);
}, [data]);
return (
//some jsx
);
}
How can i rewrite the above code to use one useMemo. could someone help me with this. i am new to using react. thanks.
Share Improve this question asked Feb 10, 2021 at 20:41 stackuserstackuser 2191 gold badge5 silver badges16 bronze badges1 Answer
Reset to default 7Like any react hook, you can return an array of values or an object of values from useMemo.
Array version
const App = () => {
const [isChecked, details, types] = React.useMemo(() => {
const source = get(data, 'somesource');
const details = get(data, 'details');
const types = get(data, 'types', []);
return [source === 'source1' || source === 'source2', details, types];
}, [data]);
return (
//some jsx
);
}
Object version
const App = () => {
const {isChecked, details, types} = React.useMemo(() => {
const source = get(data, 'somesource');
const details = get(data, 'details');
const types = get(data, 'types', []);
return {
isChecked = (source === 'source1' || source === 'source2'),
details,
types
};
}, [data]);
return (
//some jsx
);
}
本文标签: javascriptHow to return everything from one useMemo using reactStack Overflow
版权声明:本文标题:javascript - How to return everything from one useMemo using react? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744681510a2619437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论