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组件渲染显示页面

Miniflow
Miniflow

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详解

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

42

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

46

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

202

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

341

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

16

2026.01.23

php远程文件教程合集
php远程文件教程合集

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

100

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

73

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

75

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

67

2026.01.22

热门下载

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

精品课程

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

共58课时 | 4.1万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.4万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3万人学习

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

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