简体   繁体   中英

Refactoring destructuring statement

const question = this.props.question
const answer = question.answer

Refactor the above snippet to use destructuring.

const { question } = this.props
const { answer } = question

How can I refactor this destructuring to one line? If I do this one, const { question: { answer } } = this.props , I won't get the reference for question ?

You can reference the same property multiple times when destructuring:

const { question, question: { answer } } = this.props;

 const props = { question: { answer: 'answer' } }; const { question, question: { answer } } = props; console.log(question); console.log(answer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM