我的重新选择功能怎么了?我没有输出
我想使用重新选择。我想通过ID来获得购物车。
Reselect.ts
import { createSelector } from "reselect";
import { RootState } from "../store";
export const shoppingCarts = (state: RootState) => state.ShoppingCart;
export const getCartById = (state: any, id: string) => createSelector(
shoppingCarts,
state => state.find(cart => cart.product?.id === id)
);
index.tsx
const { product_id } = props;
const shoppingCart = useSelector(state => getCartById(state, product_id));
console.log(shoppingCart);
控制台日志输出
[Function memoized]
如何获得函数的输出(或JSON值)?
如果我这样做
shoppingCart()
,那我就知道
undefined is not an object (evaluating 'state.ShoppingCart')
I want to use reselect. I want to get my shopping cart by the ids.
reselect.ts
import { createSelector } from "reselect";
import { RootState } from "../store";
export const shoppingCarts = (state: RootState) => state.ShoppingCart;
export const getCartById = (state: any, id: string) => createSelector(
shoppingCarts,
state => state.find(cart => cart.product?.id === id)
);
Index.tsx
const { product_id } = props;
const shoppingCart = useSelector(state => getCartById(state, product_id));
console.log(shoppingCart);
console log output
[Function memoized]
how can I get the output of my function (or the json value) ?
if I make this
shoppingCart()
then I get this
undefined is not an object (evaluating 'state.ShoppingCart')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
getCartbyId
是选择器出厂功能,而不是选择器。这就是为什么您获得[函数记忆]
log时 incode> use exelector 时。看看Doc 如何创建一个参数的选择器?。
例如
因此,应该是:
测试
日志:
Because
getCartById
is a selector factory function, NOT a selector. That's why you got[Function memoized]
log when you called it insideuseSelector
.Take a look at the doc How do I create a selector that takes an argument?.
E.g.
So, It should be:
Test
The logs: