reactjs - 如何在 reactJS 中正确创建可重用的 useLocation 函数
问题描述
我的许多组件中都有此代码
useLocation().pathname === `/${l.toLowerCase()}`;
现在我想创建一个函数并将这段代码放在那里,以便我只能在所有必需的组件中使用该函数。
我使用以下代码创建了 isPathMatching.jsx。
import { useLocation } from "react-router-dom";
const isPathMatching = (language) => {
return useLocation().pathname === `/${language.toLowerCase()}`;
};
export default { isPathMatching };
我认为它应该可以工作,但是当我尝试导入其他组件时,它给了我以下错误:尝试导入错误:'isPathMatching' 不是从'../../utils/isPathMatching' 导出的。我检查了,我的导入路径是正确的。
我该如何解决这个问题?
解决方案
我以这种方式更改了 isPathMatching.jsx 文件,它开始工作:
export const isPathMatching = (useLocation, language) => {
return useLocation.pathname === `/${language.toLowerCase()}`;
};
推荐阅读
- javascript - 渲染进程的electron crashed事件触发太晚?
- c - 将字符串存储在 C 结构中
- google-apps-script - GOOGLE SHEETS : 测试单元格颜色和输出值
- javascript - 如何禁用 Ctrl+P 和 Ctrl+S?在 ng2-pdfjs-viewer 中。现在任何人都可以使用密钥打印或下载。任何建议将不胜感激
- javascript - 如何根据起始字母过滤数组
- java - 我做错了什么导致此图表滚动不正确?
- go - 如何使用 graphql-go 发出请求
- jquery - Jquery 选择器获取背景颜色
- c - 为什么编译 C 程序会产生这么长的二进制文件?
- python-3.x - 如何结合 tkinter GUI 和 pyglet 图形?