Reputation: 7058
What are the differences and advantages/disadvantages of the former and latter option of selecting data? When to use which option?
1) option
getDataById= (id) => createSelector(
getAllMyData,
data => data.find(d => d.id === id)
);
store.pipe(select(getDataById(myId)));
2) option
store.pipe(select(getAllDataMyData)).find(d => d.id === myId)
Upvotes: 1
Views: 1256
Reputation: 60518
The benefits to Option 1 are:
The created selector is reusable. Any component anywhere in the application can use the created selector without repeating its specific code.
The created selector is encapsulated (assuming you have it in a separate file). The components don't need to know how to access specific data in the store. If you later have to rearrange the store (breaking down the state differently, for example), you only have to modify the selector ... NOT any of the components.
Here is a summary with more:
Upvotes: 4