在React中解构和清理变量

时间:2019-04-26 16:01:45

标签: javascript reactjs destructuring

有关在React中进行销毁的快速问题

state = {
 cat: 'meow',
 dog: 'bark',
 fish: 'what sound do i make?',
 snake: 'im not wanted for this experiment',
 turtle: 'im also not wanted for this experiment'
}

const {cat, dog, fish } = this.state;
const animals = { cat, dog, fish };
this.pets(animals)

这显然只是一个例子,但是当我通过重新键入相同的变量进行结构分解时,我发现自己处于类似的情况。我希望有更简单的方法吗?像这样:

const animals = {cat, dog, fish} = this.state

还是将变量直接传递到函数中是一种不好的做法? (最终可能是一长串变量)

const {cat, dog, fish} = this.state
this.pets(cat, dog, fish}

0 个答案:

没有答案