Skip to content

Instantly share code, notes, and snippets.

@luokailuo
Created March 28, 2016 03:20
Show Gist options
  • Save luokailuo/51917dd368fd80a9c4a1 to your computer and use it in GitHub Desktop.
Save luokailuo/51917dd368fd80a9c4a1 to your computer and use it in GitHub Desktop.
“...props”属性扩散的使用
/**
* 有时候需要给组件设置多个props,却又不想一个个写下这些属性,此时react的spread attitudes功能就能派上用场了
*/
export default class UserManagement extends React.Component{
render() {
var users = {};
users.name = 'richard';
users.age = 23;
users.phone = 18898552252;
return(
<div>
<Test {...users} />
</div>
)
}
}
class Test extends React.Component{
render() {
return(
<div>{this.props.name}</div>
// output:richard
)
}
}
//同时,属性也可以被覆盖
<Test {...users} name={ 'allen' }/> //output: ‘allen’
//写在后面的属性值会覆盖前面的属性
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment