创建纯展示组件,无法使用State,也无法使用组件的生命周期方法,只负责根据传入的props来展示,不涉及到要state状态的操作,是一个只带有一个render方法的组件类
创建形式:
import React from 'react'
function NoStatusComp (props) {
console.log(props)
return (
<div>
<h1>我是无状态组件</h1>
</div>
)
}
export default NoStatusComp
特点:
createClass本质上是一个工厂函数,是ES5的原生的JavaScript来实现的React组件,是是react最初推荐的创建组件的方式。
import React from 'react'
import {Button} from 'antd'
let createClassComp = React.createClass({
getInitialState () {
return {
text: 'React早期创建组件的方式',
title: '教师节快乐,感谢生命中出现过的老师,让似水年华没有苍白,教会我们听说读写思',
num: 0
}
},
componentDidMount () {
console.log('cmd')
console.log(this.props)
},
componentWillReceiveProps (nextProps) {
console.log(this.props)
console.log('下一次')
console.log(nextProps)
},
add () {
this.num++
},
render () {
let {title, text} = this.state
return (
<div>
<h1>{title}</h1>
<span>{text}</span>
<Button onClick={this.add}> 触发方法 </Button>
</div>
)
}
})
export default createClassComp
特点:
附一篇文章 介绍的比较详细 在此不做赘述了
关于React.createClass方法与class App extends Component方法的区别
React.Component是以ES6的形式来创建react的组件的,是React目前极为推荐的创建有状态组件的方式,其成员函数不会自动绑定this,需要手动绑定,否则this不能获取当前组件实例对象。
import React from 'react'
import { Button } from 'antd'
export default class ExtendsComp extends React.Component {
constructor (props) {
super(props)
this.state = {
data: props.data
}
}
render () {
return (
<div>
<h1>我是新版创建组件的方法</h1>
<Button> 点我回去</Button>
</div>
)
}
}
特点:
React数据流动是单向的,父组件向子组件通信也是最常见的;父组件通过props向子组件传递需要的信息
import React, { Component } from 'react';
import Child from './Child';
class Parent extends Component {
render() {
return (
<div>
<Child name="apple" />
</div>
);
}
}
export default Parent;
将父组件的方法 通过props传递给子组件 然后子组件调用方法 (也就是调用了父组件的方法 进而发生改变)
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class List3 extends Component {
static propTypes = {
hideConponent: PropTypes.func.isRequired,
}
render() {
return (
<div>
我是字组件
<button onClick={this.props.show}>点我 展示</button>
</div>
);
}
}
export default List3;
context是一个全局变量,像是一个大容器,在任何地方都可以访问到,我们可以把要通信的信息放在context上,然后在其他组件中可以随意取到;
官方说明:
Context 通过组件树提供了一个传递数据的方法,从而避免了在每一个层级手动的传递 props 属性。
在一个典型的 React 应用中,数据是通过 props 属性由上向下(由父及子)的进行传递的,但这对于某些类型的属性而言是极其繁琐的(例如:地区偏好,UI主题),这是应用程序中许多组件都所需要的。 Context 提供了一种在组件之间共享此类值的方式,而不必通过组件树的每个层级显式地传递 props
import React, { Component } from 'react'
import Child from './childOneComp'
import ChildTwoComp from './childTwoComp'
import PropTypes from 'prop-types';
class Parent extends Component {
constructor () {
super()
this.state = {
listArr: [ {
text: '题目一'
},
{
text: '题目二'
} ]
}
}
// 提供一个函数,用来返回相应的context对象
getChildContext () {
return {
color: 'red'
}
}
// 父组件声明自己支持context
static childContextTypes = {
color: PropTypes.string,
}
render () {
let {listArr} = this.state
return (
<div>
<Child name='Sara' />
{
listArr.map((item, index) => {
return <ChildTwoComp value={item.text} key={index} />
})
}
</div>
)
}
}
export default Parent
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import emitter from '../../utils/event'
class ChildTwoComp extends Component {
// 子组件声明自己要使用context
static contextTypes = {
color: PropTypes.string,
}
static propTypes = {
value: PropTypes.string,
}
constructor(){
super()
this.state ={
color:'pink'
}
}
componentDidMount() {
this.eventEmitter = emitter.addListener('colorChange', (color) => {
console.log('接受了方法');
this.setState({
color,
},()=>{
console.log(color);
});
});
}
componentWillUnmount() {
if(typeof this.eventEmitter === 'function'){
this.eventEmitter.removeListener(this.eventEmitter);
}
}
render() {
const { value } = this.props;
return (
<div>
<h1 style={{ background: this.state.color }}>1234567</h1>
<li style={{ background: this.context.color }}>
<span>{value}</span>
</li>
</div>
);
}
}
export default ChildTwoComp;
以常用的发布/订阅模式举例,借用Node.js Events模块的浏览器版实现 要求组件A的数据 传递给组件B 但是 组件A和组件B 必须要同时渲染的时候 才能使用这种方法(有些类似vue的eventBus的功能,但是没有vue的强大)
import React from 'react'
import PropTypes from 'prop-types'
import emitter from '../../utils/event'
import { Button } from 'antd'
export default class Child extends React.Component {
constructor (props) {
super(props)
this.changeColor = this.changeColor.bind(this)
}
changeColor () {
emitter.emit('colorChange', 'deepskyblue')
}
render () {
let {name} = this.props
return (
<div>
<h1>Hello, {name}</h1>
<Button onClick={this.changeColor}> 11111点我回去</Button>
</div>
)
}
}
Child.propTypes = {
name: PropTypes.string.isRequired
}
import { EventEmitter } from 'events'
export default new EventEmitter()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有