热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

react函数组件比类组件的优势在哪

web前端|前端问答react,函数组件,类组件web前端-前端问答交易所源码出售,ubuntu麒麟安装黑屏,nutch中的爬虫,php外派,青岛营销seolzwA、类组件es文件

web前端|前端问答react函数组件比类组件的优势在哪
react,函数组件,类组件
web前端-前端问答
交易所源码出售,ubuntu麒麟安装黑屏,nutch中的爬虫,php外派,青岛营销seolzw
A、类组件
es文件管理源码,ubuntu解压加密文件,武汉爬虫玩家群,zuul php,seo 最新标题lzw
类组件,顾名思义,也就是通过使用ES6类的编写形式去编写组件,该类必须继承React.Component
景区售票 .net源码,ubuntu编写c代码,网络爬虫常见软件,把php直接改成php3,vue中seolzw
如果想要访问父组件传递过来的参数,可通过this.props的方式去访问

在组件中必须实现render方法,在return中返回React对象,如下:

class Welcome extends React.Component { constructor(props) { super(props) } render() { return Hello,{this.props.name} }

B、函数组件

函数组件,顾名思义,就是通过函数编写的形式去实现一个React组件,是React中定义组件最简单的方式

function Welcome(props) { return Hello,{props.name};}

函数第一个参数为props用于接收父组件传递过来的参数

C、区别

针对两种React组件,其区别主要分成以下几大方向:

1、编写形式

两者最明显的区别在于编写形式的不同,同一种功能的实现可以分别对应类组件和函数组件的编写形式

函数组件:

function Welcome(props) { return Hello, {props.name}; }

类组件:

cass Welcome extends React.Component { constructor(props) { super(props) } render() { return Hello,{this.props.name} }}

2、状态管理

在hooks出来之前,函数组件就是无状态组件,不能保管组件的状态,不像类组件中调用setState

如果想要管理state状态,可以使用useState,如下:

const FunctiOnalComponent=()=> { const [count, setCount]=React.useState(0); return (

count: {count}

);};

在使用hooks情况下,一般如果函数组件调用state,则需要创建一个类组件或者state提升到你的父组件中,然后通过props对象传递到子组件

3、生命周期

在函数组件中,并不存在生命周期,这是因为这些生命周期钩子都来自于继承的React.Component

所以,如果用到生命周期,就只能使用类组件

但是函数组件使用useEffect也能够完成替代生命周期的作用,这里给出一个简单的例子:

const FunctiOnalComponent=()=> { useEffect(()=> { console.log("Hello"); } , []); return Hello,World;};

上述简单的例子对应类组件中的componentDidMount生命周期

如果在useEffect回调函数中return一个函数,则return函数会在组件卸载的时候执行,正如componentWillUnmount

const FunctiOnalComponent=()=> { React.useEffect(()=> { return ()=> { console.log("Bye"); }; } , []); return Bye,World;};

4、调用方式

如果是一个函数组件,调用则是执行函数即可:

// 你的代码 function SayHi() { return

Hello, React

}// React内部 const result = SayHi(props) //

Hello, React

如果是一个类组件,则需要将组件进行实例化,然后调用实例对象的render方法:

// 你的代码 class SayHi extends React.Component { render() { return

Hello,React

}}// React内部 const instance = new SayHi(props) // SayHi {} const result = instance.render() //

Hello, React

5、获取渲染的值

首先给出一个示例

函数组件对应如下:

function ProfilePage(props) { const showMessage=()=> { alert('Followed '+ props.user); } const handleClick=()=> { setTimeout(showMessage, 3000); } return ()}

类组件对应如下:

class ProfilePage extends React.Component { showMessage() { alert('Followed '+ this.props.user); } handleClick() { setTimeout(this.showMessage.bind(this), 3000); } render() { return }}

两者看起来实现功能是一致的,但是在类组件中,输出this.props.user,Props在 React中是不可变的所以它永远不会改变,但是 this 总是可变的,以便您可以在 render 和生命周期函数中读取新版本

因此,如果我们的组件在请求运行时更新。this.props 将会改变。showMessage方法从“最新”的 props 中读取 user

而函数组件,本身就不存在this,props并不发生改变,因此同样是点击,alert的内容仍旧是之前的内容

小结

两种组件都有各自的优缺点

函数组件语法更短、更简单,这使得它更容易开发、理解和测试;而类组件也会因大量使用 this而让人感到困惑

类组件的性能消耗比较大,因为类组件需要创建类组件的实例,而且不能销毁。

函数式组件性能消耗小,因为函数式组件不需要创建实例,渲染的时候就执行一下,得到返回的react元素后就直接把中间量全部都销毁。


推荐阅读
author-avatar
123456ws1043
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有