原因:1、函数组件语法更短、更简单,这使得它更容易开发、理解和测试;2、类组件过多的使用this让整个逻辑看起来很混乱;3、hooks功能也只支持函数组件;4、react团队针对函数组件做了更多的优化来避免非必要的检查和内存泄漏;5、函数式组件性能消耗小,因为函数式组件不需要创建实例,渲染的时候就执行一下,得到返回的react元素后就直接把中间量全部都销毁。
本教程操作环境:windows7系统、react18版、dell g3电脑。
当使用react框架开发的时候,有两种方式创建组件,使用函数和使用类,目前函数组件越来越流行。下面通过举例的方式,分析函数组件和类组件的不同,并总结一下使用函数组件的原因(优势)。
jsx渲染方式函数组件和类组件处理jsx的方式不同,就像他们的名字,函数组件是一个纯javascript函数,直接返回jsx;而类组件是一个javascript类,通过扩展react.component,并实现render方法,render方法中返回jsx。下面举例说明:
import react from react; const functionalcomponent = () => { return <h1>hello, world</h1>; };
上面通过es6箭头函数的形式定义了一个函数组件,函数体内直接返回jsx。如果你对箭头函数不熟悉,也可以写成下面这种形式:
import react from react;function functionalcomponent() { return <h1>hello, world</h1>; }
两种写法是一样的。
然后,来看看如何定义类组件,首先我们需要扩展react.component,然后在render方法中返回jsx,具体看下面的代码片段:
import react, { component } from react;class classcomponent extends component { render() { return <h1>hello, world</h1>; }}
上面使用了es6的解构赋值语法来导入模块,如果你对解构赋值语法不熟悉,也可以写成下面这种形式,会看上去更简洁一些:
import react from react;class classcomponent extends react.component { render() { return <h1>hello, world</h1>; } }
传递props当需要向一个组件传递数据的时候,我们使用props,比如<functionalcomponent name="shiori" />,name就是component的一个props属性,这里可以有更多属性。functionalcomponent组件的函数形式的定义如下:
const functionalcomponent = ({ name }) => { return <h1>hello, {name}</h1>; };
或者不使用解构赋值
const functionalcomponent = (props) => { return <h1>hello, {props.name}</h1>; };
这种方式,你需要使用props.name来获取name属性。
然后,我们来看看类组件如何使用props,
class classcomponent extends react.component { render() { const { name } = this.props; return <h1>hello, { name }</h1>; }}
在类组件中,你需要使用this来获取props,然后可以使用解构赋值获取name属性。
处理state在react项目中,我们不可避免的要处理状态变量。类组件直到最近才支持处理状态,然而,从react从16.8版本开始,函数组件支持钩子方法usestate,这样我们可以很方便的在函数组件中使用状态变量。下面通过一个counter计数器实例来说明它们的不同。
在函数组件中处理状态变量const functionalcomponent = () => { const [count, setcount] = react.usestate(0); return ( <div> <p>count: {count}</p> <button onclick={() => setcount(count + 1)}>click</button> </div> );};
这里使用usestate钩子,它接收一个初始的state作为参数。在本例中,计数器从0开始,所以我们给count一个初始值0。
state的初始值支持各种数据类型,包括null,string或者object对象,只要是javascript允许的都可以。在=号的左边,我们使用解构赋值的形式来接受usestate的返回值,包括当前的状态变量和更新该变量的setter函数,即count和setcount。
在类组件中处理状态变量class classcomponent extends react.component { constructor(props) { super(props); this.state = { count: 0 }; } render() { return ( <div> <p>count: {this.state.count} times</p> <button onclick={() => this.setstate({ count: this.state.count + 1 })}> click </button> </div> ); }}
与函数组件大同小异,首先我们要理解react.component的构造函数constructor,react的官方文档对constructor的定义如下:
“the constructor for a react component is called before it is mounted. when implementing the constructor for a react.component subclass, you should call super(props) before any other statement. otherwise, this.props will be undefined in the constructor, which can lead to bugs.”
翻译一下,
react组件的constructor方法会在组件完全加载完成之前调用。在constructor方法中,你需要在第一行调用super(props),否则会报this.props是undefined的错误。
如果在类组件中,你没有实现constructor方法并调用super(props),那么所有的状态变量都将是undefined。所以,别忘记先定义constructor方法,在constructor方法中,我们需要给this.state一个初始值,像上面的代码那样。然后我们可以在jsx中使用this.state.count来获取count的值,setter的使用也是类似的。
这里先定义一个onclick方法,后面会用到,
onclick={() => this.setstate((state) => { return { count: state.count + 1 }; })}
这里注意setstate()方法接收的是个箭头函数,而箭头函数的参数是state和props,props是可选的,这里没用到就没写。
生命周期函数react的组件在它整个的渲染的过程中,有它的生命周期。如果你之前一直使用类组件,刚刚接触函数组件,你可能会疑惑,为什么在函数组件中没有componentdidmount()这类的生命周期方法?但是别急,有其他的钩子函数可以使用。
加载阶段类组件的生命周期函数componentdidmount会在首次渲染完成之后调用。首次渲染完成之前会调用componentwillmount ,但是这个方法在新版本的react中不推荐使用了。
在函数组件中,我们使用useeffect钩子函数来处理生命周期内的事件,像下面这样,
const functionalcomponent = () => { react.useeffect(() => { console.log(hello); }, []); return <h1>hello, world</h1>;};
useeffect有两个参数,第一个是箭头函数,第二个是[],[]里面是变化的state(s)。什么意思呢?就是[]中的状态变化了,箭头函数会被调用。如果像现在这样写个[],那箭头函数只会在组件第一次渲染之后调用一次,其功能类似下面类组件的componentdidmount。
class classcomponent extends react.component { componentdidmount() { console.log(hello); } render() { return <h1>hello, world</h1>; }}
卸载阶段const functionalcomponent = () => { react.useeffect(() => { return () => { console.log(bye); }; }, []); return <h1>bye, world</h1>;};
这里注意return的也是一个箭头函数,这个函数就是在卸载阶段执行的。当你需要执行一些卸载操作,可以放在这里,比如你可以把clearinterval放在这里,避免内存泄漏。使用useeffect钩子函数的最大好处就是可以把加载函数和卸载函数放在一个同一个地方。这里对比一下类组件的写法:
class classcomponent extends react.component { componentwillunmount() { console.log(bye); } render() { return <h1>bye, world</h1>; }}
总结函数组件和类组件各有优缺点,但函数组件相比类组件的优势:
函数组件语法更短、更简单,这使得它更容易开发、理解和测试;而类组件也会因大量使用 this而让人感到困惑
类组件过多的使用this让整个逻辑看起来很混乱;
react团队主推的react hooks功能也只支持函数组件;
react团队针对函数组件做了更多的优化来避免非必要的检查和内存泄漏;注:react团队也在努力将hooks功能引入类组件,所以没必要将现有的类组件都改写成函数组件;
类组件的性能消耗比较大,因为类组件需要创建类组件的实例,而且不能销毁。
函数式组件性能消耗小,因为函数式组件不需要创建实例,渲染的时候就执行一下,得到返回的react元素后就直接把中间量全部都销毁。
【相关推荐:redis视频教程、编程入门】
以上就是react为什么推荐函数组件的详细内容。