You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Reselect中传递多个参数?React中useSelector使用示例

Reselect 多参数传递的正确写法

你当前的选择器写法存在语法错误,而且没有正确给嵌套的selectItemsByCategory传递所需的分类参数。以下是修正后的完整实现:

正确的选择器定义

const selectChildItemByNameAndCategoryName = createSelector(
  [
    // 调用现有分类选择器,传入state和category参数
    (state, category, childName) => selectItemsByCategory(state, category),
    // 获取第三个参数:子项名称
    (state, category, childName) => childName
  ],
  (filteredItemsByCategory, childName) => {
    // 用子项名称过滤已按分类筛选后的结果
    return filteredItemsByCategory.filter(item => item.name === childName);
  }
);

React中调用选择器

你的调用写法本身没问题,保持参数顺序一致即可:

const myitems = useSelector(
  (state) => selectChildItemByNameAndCategoryName(state, 'CatName', 'childName')
);

关键注意点

  • 所有输入选择器都可以接收传递给最终选择器的全部参数,所以要在输入选择器的参数列表里声明所有需要用到的变量(比如state, category, childName),哪怕当前选择器只用到其中一部分。
  • 嵌套调用其他选择器时,必须手动把它需要的参数传过去,比如这里给selectItemsByCategory传入state和category。
  • 转换函数的参数顺序和输入选择器数组的顺序严格对应,第一个参数对应第一个输入选择器的返回值,以此类推。

内容的提问来源于stack exchange,提问作者Rolly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:22:32