2017-09-25 133 views
1

我想在我的应用程序中使用reactx/redux使用redux搜索来实现搜索过滤器。我得到的第一个问题是当我尝试添加存储增强器时,如示例中所示。如何实现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) 
 
    } 
 
    }) 
 
)

我明白evarything到resourceSelector,当我试图让一个深入了解的例子来看看它是如何工作的,但我可以勉强看到它们是如何产生和最后一行返回一个错误,无法读取未定义

我的状态对象的特性“GET”看起来像这样

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

任何人的帮助,谁明白终极版搜索是有帮助的

回答

1

如果该行:

return state.resources.get(resourceName) 

导致此错误:

Cannot read property 'get' of undefined

这表明state.resources未定义。当然,你的状态没有定义resources属性。

的示例是用意念写在心中用redux-search索引多种类型的资源,例如:

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

解决您报告将是问题之一:

  • 答:添加一个中介resources对象(如果您认为您可能想在未来编制其他内容并且您喜欢该组织)。
  • B:将state.resources.get(resourceName)替换为state[resourceName]或类似的。
+0

谢谢,你有一个伟大的图书馆,但很少有人了解它 – Craques

+0

对不起@brianvaughn,如果我来过粗鲁,它已经超过2周努力实现这一点,有点令人沮丧的我,我知道图书馆是好的这就是为什么我试图理解它。 – Craques

+0

没关系。我在这样的情况下也感到沮丧。我明白。 – brianvaughn