2017-04-20 55 views
0

道具是什么意思?代码:ReactJS中的'...道具'是什么意思?

export default class NavItem extends React.Component { 
constructor() { 
    super(); 
} 

render() { 
    const { router, index, to, children, ...props } = this.props; 
    let isActive = false; 

    if (router.isActive('./', true) && index) { 
     isActive = true 

children我想这是实际元素的孩子,但...props这是什么意思?

谢谢。

在该上下文中使用
+2

的可能的复制[什么的三个点中反应过来怎么办?(http://stackoverflow.com/questions/31048953/what-does-the-three-dots-in-react-do) –

+0

'const {children,... props} = this.props;'意思是将props.children分配给儿童,将this.props的所有剩余值分配给道具。 – elmeister

回答

1

...称为"the rest" parameter,如在“他们的休息”

这条线是使用对象Destructuring assignment语法从所述对象this.props新变量routerindextochildren分配,和其他一切(他们的休息)在this.propsprops

const { router, index, to, children, ...props } = this.props; 

这里是它的另一个例子是使用:

const bigBird = {height: 'tall', color: 'yellow', disposition: 'sunny'}; 
const {height, ...others} = bigBird; 
//height == 'tall' 
//others == {color: 'yellow', disposition: 'sunny'}; 
+0

谢谢!这是一个很大的帮助。 – JuMoGar