访问酶中的嵌套状态 [英] Access nested State in Enzyme

查看:88
本文介绍了访问酶中的嵌套状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的状态看起来像这样

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

然后我要访问fries. wrapper.state('potato')使我更上一层楼,怎么走得更深?不是

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']
  • 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)

所有退货

未定义

我不明白...

推荐答案

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

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

wrapper.state().potato.fries

wrapper.state('potato').fries

您也可以使用方括号表示法.

You can also use bracket notation.

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

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

这篇关于访问酶中的嵌套状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆