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.

1 Answer 1

2

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()
);
Sign up to request clarification or add additional context in comments.

1 Comment

It's called optional chaining

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.