reduxSearch.md
January 29, 2016 ยท View on GitHub
reduxSearch
Typically redux-search can be configured to index documents using an array of searchable field names.
const store = compose(
reduxSearch({
// Other configuration goes here...
resourceIndexes: {
// Search configuration here (eg. search all Books by :title and :author)
books: ['author', 'title']
}
})
)(createStore)(rootReducer)
Sometimes you may want to index on computed values. Or perhaps your collection's attributes can't be accessed using simple bracket notation. In that case you can provide your own custom indexing function. For example, a book with multiple related authors might be indexed like so:
const store = compose(
reduxSearch({
// Other configuration goes here...
resourceIndexes: {
books: ({ resources, indexDocument, state }) => {
resources.forEach(book => {
indexDocument(book.id, book.name)
book.authors.forEach(
author => indexDocument(book.id, author.name)
)
})
}
}
})
)(createStore)(rootReducer)
You can use the indexDocument callback to index your collection on as many fields as you would like. Indexing is done in a web-worker thread and so it won't block the UI.
A state object is also specified in case your custom index needs to access any other part of your application store.