2

I have the following code in my react/redux app:

import { createSelector } from 'reselect';

const selectMembers = state => state.membersData;

const makeSelectLessonSets = createSelector(
  selectMembers,
  substate => substate.get('myData').toJS()
);

The problem is that sometimes myData is not yet defined, so substate.get('myData') will not get anything. And since I try to call toJS() on it, it shows the error: undefined is not an object.

But I don't know how to check if substate.get('myData') has returned a valid object inside createSelector before I call toJS() on it.

Can you please help with it.

asanas
  • 3,782
  • 11
  • 43
  • 72

1 Answers1

1

I added ? before the dot this will not throw an error

import { createSelector } from 'reselect';

const selectMembers = state => state.membersData;

const makeSelectLessonSets = createSelector(
  selectMembers,
  substate => substate?.get('myData')?.toJS()
);
Daniel Bellmas
  • 589
  • 5
  • 17
  • It's called [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) – HMR Jun 21 '21 at 10:40