我useSelector
从一个组件成功调用,该组件从 id 派生产品名称。
const productId = 25; // whatever
const productName = useSelector(
(state) =>
state.dashboard.dashboards.filter(
({ Id }) => Id === productId
)[0].Name
);
但是,我的选择器依赖于productId
,我将其存储在同一文件的变量中。我想将此useSelector
调用存储在外部文件中,以便可以共享。我尝试以下,但id
为undefined
:
选择器.js
export const getProductNameById = (store, id) => {
return store.dashboard.dashboards.filter(({ Id }) => Id === id)[0]
.Name;
}
some_file.js
import { useSelector } from "react-redux";
import { getProductNameById } from "./selectors";
const productId = 25;
const productName = useSelector(getProductNameById, productId);