2

To display the page for user fast i must to show it before i fetches data if the page was refreshed. Selecting data from store can throw error like "reading property of undefined". To evade it i just use lodash get and it handles that error and returns null in reselect fucntions chain like that:

export const getSomething= createSelector(
  [data],
  (d) => _.get(d, 'property', null),
);

Now it will return data when the store is populated, but for the first second it will return null and viewer will see styled page without some data till it will be fetched and selected. Is it good practice to use _.get or there some any better solutions?

Bash Lord
  • 127
  • 1
  • 10
  • Enable support for the optional chaining operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining. https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining if you're using babel – azundo Sep 16 '19 at 22:05
  • you could also define default values. Either in your initialState or in the selector. Like: `export const getSomething= createSelector( [data], (d = {}) => d.property );` This really only woks if you don't have nested object... So chain operator or lodash is probably the way to go. – Daniel Oct 10 '19 at 09:05

0 Answers0