【发布时间】:2022-01-23 18:30:09
【问题描述】:
如果位置发生变化,我会尝试呈现特定的布局,并且我正在使用 useMemo 挂钩来验证路线并基于此值呈现,但即使路线发生变化,它也始终返回 true,例如新路线是 . ../SSDSD/草稿
const location = useLocation();
console.log(location)
const showShadow = useMemo(() => ['preview', 'signing', 'receipts'].some(word => location.pathname.includes(word)), [location]);
const isHomePage = useMemo(() => ['/', '/search', '/addressbook'].some(word => location.pathname.includes(word)), [location]);
console.log(isHomePage);
//debugger;
return (
<ThemeProvider theme={theme}>
<Box className={isHomePage ? classes.homeBackground : classes.innerBackground}>
</Box>
</ThemeProvider>
);
}
【问题讨论】:
-
尝试使用
location.pathname作为依赖 -
还是一样的``` const isHomePage = useMemo(() => ['/', '/search', '/addressbook'].some(word => location.pathname.includes(字)),[位置.路径名]); ``` 控制台 ``` console.log(location) ``` 对象哈希:"" key: "vvj7z7" 路径名:"/EP1001E2K/draft" 搜索:"" state: null [[Prototype]]: Object ` ``console.log(isHomePage); ``` Layout.js:66 真
-
在 useMemo 的回调中记录路径名并检查它是否正在更新
标签: javascript reactjs react-router-dom react-usememo