问题
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