如何解决未捕获的错误createSelector方法?

时间:2019-11-14 05:04:13

标签: javascript redux store reselect

我在Angular应用中使用了选择器,但是在调用选择器时遇到了未捕获的错误。尽管我已经提供了所需的选择器函数作为createSelector的参数。根据此处的重新选择文档-https://github.com/reduxjs/reselect

Uncaught Error: Selector creators expect all input-selectors to be functions, 
instead received the following types: [object]

我确实在这里发现了类似的问题,这表明将选择器从箭头函数更改为普通函数声明会解决该问题。为了解决函数声明的任何吊起问题,但这不能解决未捕获的错误- https://github.com/reduxjs/reselect/issues/169

通过传入store.getState()作为参数来调用选择器:

let state = getBookTitles(this.store.getState());

问题:

调用重新选择的createSelector时如何解决未捕获的错误?

这是我的Selectors.js文件,其中包含应用程序状态选择器:

// Selectors.js

import { createSelector } from 'reselect';

function selectBooks(state: any) {
  return state ? state.books: [];
}
function selectBookTitles(books: any) {
  return books ? books.titles: [];
}

// bookTitle selector
export let getBookTitles = createSelector(
  selectBooks,
  selectBookTitles,
);

1 个答案:

答案 0 :(得分:1)

发现错误来自selector.js文件中定义不正确的选择器。

错误:

Uncaught Error: Selector creators expect all input-selectors to be functions, 
instead received the following types: [object]

语法错误:

export const selectConfig = createSelector(
  [initialSettingsState.config],
  'config',
);

正确的语法:

const configSelector = state => {
  return state ? state.config: [];
};

export const getConfig = createSelector(
  configSelector,
  config=> {
    return config;
  },
);