React Redux 重新选择 - 未定义状态

React Redux Reselect - Undefined State

我的 react/redux 应用程序中有以下代码:

import { createSelector } from 'reselect';

const selectMembers = state => state.membersData;

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

问题是有时myData还没有定义,所以substate.get('myData')什么也得不到。由于我尝试在其上调用 toJS(),它显示错误:undefined is not an object.

但我不知道如何在调用 toJS() 之前检查 substate.get('myData') 是否在 createSelector 中返回了一个有效对象。

你能帮忙吗?

我在点前加了?这样不会报错

import { createSelector } from 'reselect';

const selectMembers = state => state.membersData;

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