react组件

  • 使用函数方式

  

1 // 创建组件
2 function Welcome(props) {
3   return <div>Hello, {props.name}</div>      
4 }
5 
6 // 使用组件
7 <Welcome name="明" />

  使用函数方式创建组件,父组件通过 属性 的方式传递参数,子组件函数通过 形参 接收参数

  • 使用 class 方式
 1 // 使用 class类 的方式创建组件
 2 // extends React.Component 表示 继承 React.Component 组件的功能
 3 class Welcome extends React.Component {
 4   render() {
 5     return <div>Hello, {this.props.name}</div>    
 6   }      
 7 }
 8 
 9 // 使用组件
10 <Welcome name="明" />

使用 class类 方式创建组件,父组件通过 属性 的方式传递参数,子组件通过 this.props 使用参数

注意:props 中的数据为只读属性,无法进行修改操作

posted @ 2022-11-28 15:25  萧一下  阅读(14)  评论(0编辑  收藏  举报