0

0

React入门学习:React创建组件的方法

不言

不言

发布时间:2018-08-22 17:41:50

|

1833人浏览过

|

来源于php中文网

原创

创建组件

创建组件之前要注意以下几点:

  1. 组件创建的名称首字母得大写

  2. 组件中返回的JSX只能是一个根节点,所有的内容得用一个元素都框起来

1.无状态函数式组件

无状态函数式组件可以理解成就是一个函数生成的,使得代码的可读性更好,并且精简、便利,减少了冗余,无状态组件有以下特点:

  1. 组件无法被实例化,整体渲染提高

  2. 组件不能访问this对象,因为没有实例化,所以无法访问到this对象

  3. 组件没有生命周期

  4. 无状态组件只能访问输入的props,没有state状态

import React from 'react'
import { connect } from 'dva';

 function CreateComponent(props) {
   console.log(props);
   return (
     

{props.name}今年{props.age}岁

) } export default connect(state => ({ name:'小明', age:15 }))(CreateComponent);

2.React.Component类组件

每个组件类必须要实现一个render方法,这里要特别注意,这个render方法必须要返回一个JSX元素即要用一个最外层的元素将所有内容都包裹起来,如果返回并列多个JSX元素是不合法,如下所示:

import React from 'react'

class CreateComponent extends React.Component {
     render() {
       return(
         

标题

  • 首先
  • 其次
  • 最后

) } } export default CreateComponent;

以上实例,就是把h2元素和ul用一个p都给包起来

1.组件的事件监听

import React from 'react'

class CreateComponent extends React.Component {

   clickFunc = (e) => {
     console.log("监听:",e.target.innerHTML);
   }

   clickValue = (value) => {
     console.log(value);
   }
    render() {
      return (
       

监听事件
this对象

) } } export default CreateComponent;

以上就是事件监听和传参实例

2.组件的state和setState

通常在组件中,state是用来放这个组件内部参数的状态的,而setState是用来改变state里面的参数,例如:

import React from 'react'

class CreateComponent extends React.Component {
  state = {
    flag : true
  }
   clickValue = () => {
     this.setState({
       flag: !this.state.flag
     })
   }
    render() {
      return (
       

flag的值为:{this.state.flag ? '真' : '假'}

) } } export default CreateComponent;

3.组件的props

props是组件里面的属性,在组件内部是不能更改自己本身的props的,比如,建立一个组件,然后在另外一个组件里面调用这个组件,如下:

import React from 'react';

function NewComponent(props) {
  return (
    

{props.content}

); } export default NewComponent;

建立一个组件NewComponent,然后调用,如下:

import React from 'react'
import NewComponent from './newComponent.js'

class CreateComponent extends React.Component {
    render() {
      return (
       

) } } export default CreateComponent;

从这里可以看出,props就是组件带入的属性值,props其实就是让外部组件对自己进行配置,而state是组件控制自己的状态

4.组件的生命周期

constructor组件初始化:

constructor初始化一些参数属性等等

componentWillMount组件渲染之前:

componentWillMount这个函数在react16.3.0之后慢慢的被弃用了,使用componentDidMount替换

componentDidMount组件渲染之后:

componentDidMount在组件渲染之后实行,可以加载数据

render组件渲染:

render组件渲染显示页面

ChartGen
ChartGen

AI快速生成专业数据图表

下载
import React from 'react'

class CreateComponent extends React.Component {
  constructor () {
    super()
    console.log('construct:页面初始化')
  }

  componentWillMount () {
    console.log('componentWillMount:页面将要渲染')
  }

  componentDidMount () {
    console.log('componentDidMount:页面渲染结束')
  }


    render() {
      console.log('render:页面渲染');
      return (
       

页面渲染

) } } export default CreateComponent;

输出结果:

construct:页面初始化
componentWillMount:页面将要渲染
render:页面渲染
componentDidMount:页面渲染结束
componentWillUnmount组件删除

componentWillUnmount函数是在组件要删除之前执行的函数,如下代码:

import React from 'react';

class NewComponent extends React.Component {
  componentWillUnmount() {
    console.log('componentWillUnmount:将要从页面中删除');
  }

  render() {
    return (
      

{this.props.content}

); } } export default NewComponent;

建立一个组件NewComponent,然后在CreateComponent组件中引入这个组件,如下:

import React from 'react'
import NewComponent from "./newComponent.js";

class CreateComponent extends React.Component {
  constructor () {
    super()
    console.log('construct:页面初始化');
    this.state = {
      content:'测试组件',
      isDelete:false
    }
  }

  componentWillMount () {
    console.log('componentWillMount:页面将要渲染')
  }

  componentDidMount () {
    console.log('componentDidMount:页面渲染结束')
  }

  deleteFunc = () => {
    this.setState({
      isDelete:true
    })
  }


    render() {
      console.log('render:页面渲染');
      return (
       

页面渲染 {!this.state.isDelete?( ):(null)}

) } } export default CreateComponent;

当点击删除按钮的时候,组件NewComponent会被删除,在删除之前会执行componentWillUnmount函数

输出结果:

construct:页面初始化
componentWillMount:页面将要渲染
render:页面渲染
componentDidMount:页面渲染结束
componentWillUnmount:将要从页面中删除

以上几个生命周期是我们会常用到的组件生命周期,组件的生命周期还有更新阶段的生命周期,不过这些比较少用,这里简单介绍一下:

shouldComponentUpdate(nextProps, nextState)

通过这个方法控制组件是否重新渲染,如果返回false不会重新渲染,如下

import React from 'react'
import NewComponent from "./newComponent.js";

class CreateComponent extends React.Component {
  constructor () {
    super()
    console.log('construct:页面初始化');
    this.state = {
      content:'测试组件',
      isDelete:false
    }
  }

  componentWillMount () {
    console.log('componentWillMount:页面将要渲染')
  }

  componentDidMount () {
    console.log('componentDidMount:页面渲染结束')
  }

  shouldComponentUpdate(nextProps, nextState){
    if(nextState.isDelete){
      return false;
    }

  }

  deleteFunc = () => {
    this.setState({
      isDelete:true
    })
  }


    render() {
      console.log('render:页面渲染');
      return (
       

页面渲染 {!this.state.isDelete?( ):(null)}

) } } export default CreateComponent;

此时点击删除按钮,页面没有进行渲染,那是因为在shouldComponentUpdate中设置了返回值为false,当返回值为false的时候,页面无法重新渲染。这个函数第一个参数表示最新的props,第二个参数表示最新的state

componentWillReceiveProps(nextProps)

组件从父组件接收到新的 props 之前调用,函数的参数nextProps表示接收到的数据

在NewComponent组件中:

import React from 'react';

class NewComponent extends React.Component {
  componentWillUnmount() {
    console.log('componentWillUnmount:将要从页面中删除');
  }

  componentWillReceiveProps(nextProps){
    console.log(nextProps);
  }

  render() {
    return (
      

{this.props.content}

); } } export default NewComponent;

在组件CreateComponent中:

import React from 'react'
import NewComponent from "./newComponent.js";

class CreateComponent extends React.Component {
  constructor () {
    super()
    console.log('construct:页面初始化');
    this.state = {
      content:'测试组件',
      isDelete:false
    }
  }

  componentWillMount () {
    console.log('componentWillMount:页面将要渲染')
  }

  componentDidMount () {
    console.log('componentDidMount:页面渲染结束')
  }

  changeFunc = () => {
    this.setState({
      content:'文字修改'
    })
  }


    render() {
      console.log('render:页面渲染');
      return (
       

页面渲染 {!this.state.isDelete?( ):(null)}

) } } export default CreateComponent;

不过componentWillReceiveProps将在react16.3.0开始之后弃用

componentWillUpdate:

组件重新渲染之前调用这个方法,将在react16.3.0开始之后弃用

componentDidUpdate:

组件重新渲染并且把更改变更到真实的 DOM 以后调用

注意: componentWillUpdate、componentWillReceiveProps、componentWillMount这三个生命周期将在react116.3.0之后开始弃用

相关推荐:

React组件生命周期实例分析

React组件Dragact 0.1.4详解

相关专题

更多
Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

65

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

36

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

75

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

21

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 3.6万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.2万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号