admin管理员组文章数量:1429844
I'm still unfamiliar with all the magic of ES6. I saw this code in an online article and I'm not sure how PrivateRoute
is destructuring the input props. what does ponent: Component
do in this context?
const PrivateRoute = ({ ponent: Component, ...rest }) => (
// Code here
)
I understand that I can do something like this to destructure an object
obj = {firstName: 'John', lastName: 'Doe'};
{first, last} = obj;
and have first = 'John'
, last = 'Doe'
; however, I got confused with the introduction of a colon in the example code.
Here's a link to the full article: /
I'm still unfamiliar with all the magic of ES6. I saw this code in an online article and I'm not sure how PrivateRoute
is destructuring the input props. what does ponent: Component
do in this context?
const PrivateRoute = ({ ponent: Component, ...rest }) => (
// Code here
)
I understand that I can do something like this to destructure an object
obj = {firstName: 'John', lastName: 'Doe'};
{first, last} = obj;
and have first = 'John'
, last = 'Doe'
; however, I got confused with the introduction of a colon in the example code.
Here's a link to the full article: https://tylermcginnis./react-router-protected-routes-authentication/
Share Improve this question asked Jun 12, 2018 at 21:59 YSAYSA 8792 gold badges15 silver badges33 bronze badges 1- 3 Read the docs: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – CertainPerformance Commented Jun 12, 2018 at 22:01
2 Answers
Reset to default 7There are two basic ways to use the :
in destructuring:
- destructuring sub objects
- aliasing a variable
If the right hand side of the :
is an object or array then you are destructuring a sub-object. If the right hand side is an identifier then you are aliasing the key on the left hand side of the :
Destructuring Sub Objects
const { ponent: { example } } = opts
// equivalent to
const example = opts.ponent.example
Aliasing a Variable
const { ponent: Component } = opts
// equivalent to:
const Component = opts.ponent
Both Combined
const { ponent: { example: Component } } = opts
// equivalent to
const Component = opts.ponent.example
It's to use a different name for a property obtained by destructuring.
let obj = {
a: 'thing A',
b: 'thing B'
}
let { a: newVariable } = obj
console.log(newVariable) // outputs: "thing A"
See MDN docs for Assigning to new variable names
本文标签: javascriptUsing colons within object destructuringStack Overflow
版权声明:本文标题:javascript - Using colons within object destructuring - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745476390a2659982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论