类组件和函数组件

一、类组件和函数组件

函数组件:(把它看作一种样式组件,展示样式)
函数组件只能实现非常简单的渲染功能。只是进行页面的展示和数据的渲染。没有逻辑的处理。也就是组件的内部是没有自己的数据和状态的。它是无状态组件。
通常仅是接受外部传来的数据进行展示,不涉及state,只根据props,不用this.props

export default function PickerButton(props){

  const { key1,children }=props

  return(
    <div className='picker-button'>
      <span>{key1}</span>
      <div>{children}</div>
    </div>
  )


}

【写法】写一个function并导出,return一个结构,通过props获取数据;
调用的时候,将变量值通过属性的方式注入

export default function Headertitle({key1,key2,children}){
	const 
	
	return (
		<div>helloworld  {key1}{key2}
		<div>{children}</div>.      //children为一个html结构(div。。。。)

		</div>
	)


}
<HeaderTitle 
	key1='daming',
	key2='12',
	children={<div>你好</div>}
/>

类组件
在里面可以写入状态、生命周期、构造函数
写法:继承React.componnent

export default HeaderTitle extends React.component {
	constructor(props){
		super(props)          //constructor和 super同时必须有(不写也可,会默认有)
		this.state={
		name:'daming',
		age:12,
		sex:man
	}
	this.showname=  function() {     //这种写法绑定在实例对象上
        console.log(this.name)
       }                   
	}
	showAge = () => {                 //这种写法绑定在实例对象上
		console.log('age')
	}
	showSex(){                      //这种写法在原型对象上,class的prototype属性
		console.log('sex')
	}
	render(
	const {key1,key2}=this.props     //需要数据由此传递
	return<div>{key1}{key2}</div>)
}

<HeaderTitle 
	key1='daming'
	key2='12'
/>

super调用父类的构造器;
super必须在this之前使用
在这里插入图片描述

参考:
ES6中的class的详解

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值