Access nested State in Enzyme

泪湿孤枕 提交于 2019-12-02 22:33:46

问题


My state looking like this

this.state = { 
    potato: {
        chips: 'yum',
        fries: 'even better',
    }
}

Then I want to access fries. wrapper.state('potato') get me to the first level, how to go deeper? It is not

  • wrapper.state('potato').state('fries')
  • wrapper.state('potato', 'fries')
  • wrapper.state(['potato', 'fries'])
  • wrapper.state('potato').fries
  • wrapper.state('potato')['fries']

When I do

const potato = wrapper.state('potato');

and then

console.log(potato);

I get

{
     chips: 'yum',
     fries: 'even better',
}

However, accessing it via

console.log(potato.chips);
console.log(potato.fries);
console.log(potato.potato.chips)

all returns

undefined

I dont get it...


回答1:


All you have to do is get the state at first and access the property by using dot notation.

i.e

wrapper.state().potato.fries

wrapper.state('potato').fries

You can also use bracket notation.

i.e.

wrapper.state()['potato']['fries']

wrapper.state('potato')['fries']



来源:https://stackoverflow.com/questions/54622739/access-nested-state-in-enzyme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!