Galivan
Galivan

Reputation: 5348

Redux - if property from state is null (Typeerror)

If I'm trying to get a value from a property from state, and there is no such property, I get TypeError: state.session.user.designer.account is null

ShopSetupForm = connect(
    state => ({
        initialValues: {
             account_bank: state.session.user.designer.account.bank

So in the case where state.session.user.designer.account is not set in state, those values I'm trying to set is null and the page doesn't load.

Should I make some condition? This didn't work:

account_bank: state.session.user.designer.account.bank || ''

or

 account_bank: state.session.user.designer.account.bank ? state.session.user.designer.account.bank : ''},

Upvotes: 0

Views: 50

Answers (1)

Lucas
Lucas

Reputation: 4097

The error is because you're trying to access the property bank from state.session.user.designer.account when it's null. In that case, the fix would be to check if the value is not null and then access the property:

account_bank: state.session.user.designer.account && state.session.user.designer.account.bank

(this would evaluate to account_bank: null) or

account_bank: state.session.user.designer.account ? state.session.user.designer.account.bank : ''

(which would evaluate to account_bank: '').

Upvotes: 1

Related Questions