const question = this.props.question
const answer = question.answer
重构上面的代码片段以使用解构。
const { question } = this.props
const { answer } = question
如何将这种解构重构为一行?
如果我这样做,
const { question : { answer } } = this.props
, 我不会得到
question
的引用?
请您参考如下方法:
解构时可以多次引用同一个属性:
const { question, question: { answer } } = this.props;
const props = {
question: {
answer: 'answer'
}
};
const { question, question: { answer } } = props;
console.log(question);
console.log(answer);