小编典典

访问酶中的嵌套状态

reactjs

我的状态看起来像这样

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

然后我要访问frieswrapper.state('potato') 使我达到第一个水平,如何更深入?它不是

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

当我做

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

然后

console.log(potato);

我懂了

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

但是,通过访问

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

所有回报

未定义

我不明白…


阅读 243

收藏
2020-07-22

共1个答案

小编典典

您要做的就是首先获取状态并使用点表示法访问属性。

wrapper.state().potato.fries

wrapper.state('potato').fries

您也可以使用括号表示法。

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

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

2020-07-22