unistore icon indicating copy to clipboard operation
unistore copied to clipboard

Best practice for "filter" functions?

Open jeberly opened this issue 7 years ago • 2 comments

What is the best practice for functions that don't alter state, but just return a subset of state? Eg. show todos that start with 'a'? Or if I have a cart that is an array of product ids, and I want the array of products based on those ids.

Thanks in advance.

jeberly avatar Feb 08 '18 04:02 jeberly

@jeberly what's about using reselect for example ? I mean, I think we should filter/sort our data right before rendering it. The state should remains the same. By this way we can use the same data to rendering on different components plus different filters and with no data replication.

horaciosystem avatar Feb 08 '18 23:02 horaciosystem

+1 to @horaciosystem's point about avoiding duplicated data. It seems like those 2 example problems would be best suited to a selector:

@connect( (state, props) => {
  let todos = state.todos.filter( todo => todo.text.indexOf(props.search) !== -1 );
  return { todos };
})
class List extends Component {}

developit avatar Apr 27 '18 02:04 developit