没有props属性的时候:
此时应该要使用props:
发现元素react组件自带props属性,还有refs属性
当给我们自己写的react组件添加一个属性时
发现该属性被添加在props里了
传多个Props属性:
案例2
写了三次组件标签,就会创建三次实例,就会调用三次render,都输出一次this
props使用的简写:
案例,将 State 作为 Props 传递给子组件
class MyApp extends React.Component {
constructor(props) {
super(props);
this.state = {
name: 'CamperBot'
}
}
render() {
return (
<div>
<Navbar name={this.state.name}/* your code here */ />
</div>
);
}
};
class Navbar extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>Hello, my name is: {this.props.name}</h1>
</div>
);
}
};