Javascript 不可变getIn返回null,但不返回notSetValue
我有一个简单的州记录:Javascript 不可变getIn返回null,但不返回notSetValue,javascript,redux,immutable.js,Javascript,Redux,Immutable.js,我有一个简单的州记录: interface StateParams { priceObj: PriceDTO; } const defaultState = { priceObj: undefined, } class State extends Record(defaultState) implements StateParams { priceObj: PriceDTO; } 默认情况下,它具有未定义的值 无论发生什么,作为要约请求的结果都会保存到priceObj,而不会转
interface StateParams {
priceObj: PriceDTO;
}
const defaultState = {
priceObj: undefined,
}
class State extends Record(defaultState) implements StateParams {
priceObj: PriceDTO;
}
默认情况下,它具有未定义的值
无论发生什么,作为要约请求的结果都会保存到priceObj
,而不会转换为不可变
(state: State, priceObj: PriceDTO) => state.set('priceObj', priceObj),
我还有一个简单的选择器:
export const getQuoteId = (state: State): string | undefined =>
state.getIn(['offerData', 'priceObj'], {}).offerID;
不幸的是,它抛出了一个错误:
uncaughttypeerror:无法读取null的属性“offerID”
在这种情况下,为什么
getIn
不返回notSetValue
值,有什么特别的原因吗?当您通过减速机中的set操作添加键时,状态包含键priceObj
notSetValue
仅在映射不包含键时返回。显式的null
是一个有效值(正如未定义的),因此{}
不会返回
您应该选中priceObj!=设置为“状态”或使选择器类似于以下内容之前,null
:
export const getQuoteId = (state: State): string | undefined =>
(state.getIn(['offerData', 'priceObj']) || {}).offerID;