如何实现redux-search

时间:2017-09-25 18:55:33

标签: redux

我正在尝试在我的应用程序中实现一个使用redux-search的react / redux的搜索过滤器。我得到的第一个问题是当我尝试添加商店增强器时,如示例中所示。



// Compose :reduxSearch with other store enhancers
const enhancer = compose(
  applyMiddleware(...yourMiddleware),
  reduxSearch({
    // Configure redux-search by telling it which resources to index for searching
    resourceIndexes: {
      // In this example Books will be searchable by :title and :author
      books: ['author', 'title']
    },
    // This selector is responsible for returning each collection of searchable resources
    resourceSelector: (resourceName, state) => {
      // In our example, all resources are stored in the state under a :resources Map
      // For example "books" are stored under state.resources.books
      return state.resources.get(resourceName)
    }
  })
)




我理解evadthing到resourceSelector,当我试图深入了解它的例子以了解它是如何工作但我几乎看不到它们是如何生成的并且最后一行返回错误,无法读取属性'让'未定义的

我的状态对象看起来像这样



state: {
  //books is an array of objects...each object represents a book
  books:[
  //a book has these properties
  {name, id, author, datePublished}
  ]
}




任何了解redux-search的人都可以提供帮助

1 个答案:

答案 0 :(得分:1)

如果这一行:

return state.resources.get(resourceName)

导致此错误:

  

无法阅读财产' get'未定义的

这表明state.resources未定义。当然,您的州没有定义resources属性。

这些示例的编写考虑了使用redux-search索引许多类型的资源,例如:

state: {
  resources: {
    books: [...],
    authors: [...],
    // etc
  }
}

您报告的问题的解决方案是:

  • 答:添加一个中间resources对象(如果您认为将来可能想要将其他内容编入索引并且您喜欢该组织)。
  • B:将state.resources.get(resourceName)替换为state[resourceName]或类似。
相关问题