正在访问数组解构中的特定元素?

问题描述 投票:0回答:1
const { navigation, currentScreen, childIndex } = this.state
const screen = navigation[currentScreen]

如何在同一行中编写这两行代码?

javascript reactjs destructuring
1个回答
2
投票

使用{ [currentScreen]: screen },请确保您事先提取了currentScreen

const state = { navigation: { foo: 'val' }, currentScreen: 'foo' };

const { navigation, currentScreen, childIndex, navigation: { [currentScreen]: screen } } = state
console.log(screen);
© www.soinside.com 2019 - 2024. All rights reserved.