当前位置: 首页 > news >正文

南通网站建设招聘做网页用什么软件好

南通网站建设招聘,做网页用什么软件好,可视化网站模板,郑州 网站建设:react代码模块分为类组件和函数组件。 从语法和定义、内部状态管理、生命周期、性能、可读性和维护性、上下文、集成状态管理库等角度对比React中类组件和函数组件。 1、语法和定义 类组件: 使用 ES6 的类(class)语法定义的 React 组件。…

react代码模块分为类组件和函数组件。

语法和定义内部状态管理生命周期性能可读性和维护性上下文集成状态管理库等角度对比React中类组件和函数组件。

1、语法和定义

类组件:

使用 ES6 的类(class)语法定义的 React 组件。它们具有更复杂的功能,特别是在 React 16.8 之前,它们是唯一能够使用状态(state)和生命周期方法的组件。

特点:

  • 使用 class 关键字定义,并继承自 React.Component
  • 能够使用 state 来管理组件的内部状态。
  • 可以使用生命周期方法,如 componentDidMountcomponentDidUpdate componentWillUnmount 等。
  • 通过 this.props 来访问传递给组件的属性(props)。
import React, { Component } from 'react';class HelloComponent extends Component {constructor(props) {super(props);this.state = {count: 0};}increment = () => {this.setState({ count: this.state.count + 1 });}render() {return (<div><p>Count: {this.state.count}</p><button onClick={this.increment}>Increment</button></div>);}
}export default HelloComponent;

函数组件(Function Component):

函数组件是使用 JavaScript 函数定义的 React 组件。自 React 16.8 以来,函数组件通过引入 Hooks 可以实现状态管理和副作用处理,功能上已经与类组件基本等价。 

特点:

  • 使用 JavaScript 函数定义,通常是更简单和推荐的方式。
  • 在函数组件中,无法直接使用 this,而是通过 Hooks(如> useState useEffect)来管理状态和副作用。
  • 更加简洁,通常用于无状态组件,但在有状态需求时也可以使用 Hooks
import React, { useState } from 'react';function HelloComponent() {const [count, setCount] = useState(0);const increment = () => {setCount(count + 1);};return (<div><p>Count: {count}</p><button onClick={increment}>Increment</button></div>);
}export default HelloComponent;

 

2、内部状态管理

类组件:

使用类组件时,可以通过组件的内部状态(state)来管理组件的数据。

import React from 'react';class Counter extends React.Component {constructor(props) {super(props);this.state = { count: 0 };}render() {return (<div><p>Count: {this.state.count}</p><button onClick={() => this.setState({ count: this.state.count + 1 })}>Increase</button></div>);}
}export default Counter;

函数组件(Function Component):

函数组件可以使用hooks(React 16.8+)来管理内部状态。

useState钩子:

import React, { useState } from 'react';function Counter() {const [count, setCount] = useState(0);return (<div><p>Count: {count}</p><button onClick={() => setCount(count + 1)}>Increase</button></div>);
}export default Counter;

useReducer钩子(适用于复杂的状态逻辑):

import React, { useReducer } from 'react';function Counter() {const [count, dispatch] = useReducer((state, action) => {switch (action.type) {case 'increment':return state + 1;case 'decrement':return state - 1;default:throw new Error();}}, 0);return (<div><p>Count: {count}</p><button onClick={() => dispatch({ type: 'increment' })}>Increase</button><button onClick={() => dispatch({ type: 'decrement' })}>Decrease</button></div>);
}export default Counter;

3、生命周期

类组件:

类组件生命周期(三个阶段:挂载阶段,更新阶段,卸载阶段)

挂载阶段执行顺序 :constructor->componentWillMount->render->componentDidMount

更新阶段执行顺序: shouldComponentUpdate->componentWillUpdate->render->componentDidUpdate

销毁阶段: componentWillUnmount

import React  from "react";
import {Link} from 'react-router-dom'
class Demo extends React.Component{constructor(props){super(props)console.log("constructor")}state = {num:1}UNSAFE_componentWillMount(){console.log("componentWillMount")}componentDidMount(){console.log("componentDidMount")}shouldComponentUpdate(){console.log('shouldComponentUpdate')return true}UNSAFE_omponentWillUpdate(){console.log("componentWillUpdate")}componentDidUpdate(){console.log("componentDidUpdate")}componentWillUnmount(){console.log("componentWillUnmount")}Submit = () =>  {this.setState({num:this.state.num+=1})}render(){console.log('render')return(<><Link to='/app1'>App1</Link>//这里替换成自己的即可<h3>{this.state.num}</h3><button onClick={this.Submit}>改变</button></>)}
}
export default Demo

函数组件(Function Component):

函数组件本质没有生命周期,但是我们通过Hooks来模仿类组件当中的生命周期(也是三个阶段)

import { useState ,useEffect} from "react"
function App1 (){const [username ,setUsername] = useState('')const setChange = event => {setUsername(event.target.value);};// useEffect  =  vue mounted 区别是只要视图更新就变化 感觉类似watch 但是他又是初始化的 时候第一个//  useEffect(()=>{},[])useEffect(()=>{console.log('模拟componentDidMount第一次渲染', username)return () =>{console.log('模拟componentWillUnmount执行销毁后')}},[username])return(<><input value={username} onChange={setChange}></input></>)
}
export default  App1

4、性能

React类组件和函数组件之间的性能对比主要关注以下几个方面:

角度类组件函数组件
渲染性能在渲染时会进行shouldComponentUpdate生命周期检查需要使用React.memo或自定义比较逻辑来避免不必要的重渲染
状态更新可以在shouldComponentUpdate中实现更细粒度的更新检查需要使用React.memo或自定义比较逻辑来避免不必要的重渲染
组件的状态和生命周期复杂性可能会引入更多的对象和函数,这可能会影响GC(垃圾回收)-
内存占用因为其实例和可能的引用,可能会占用更多内存-

5、可读性和维护性

可读性和维护性是评估代码质量的重要指标。

  • 类组件的可读性和维护性可能不如函数组件。类组件中的this指向以及生命周期方法(如componentDidMount)可能会增加理解和维护的难度。

  • 函数组件的可读性和维护性较好,因为它们就像纯函数,更接近于数学函数的定义,更容易理解和维护。

  • 对于简单的组件,使用函数组件是首选,因为它们更简单且易于理解。

  • 对于需要管理状态或生命周期事件的组件,类组件是必须的。

  • 可以使用hooks(React 16.8+)来编写函数组件,它们可以让你在不编写类的情况下使用state和其他React特性。

如:类组件

class MyComponent extends React.Component {constructor(props) {super(props);this.state = { count: 0 };}render() {return (<div><p>You clicked {this.state.count} times</p><button onClick={() => this.setState({ count: this.state.count + 1 })}>Click me</button></div>);}
}

函数组件(Function Component):

function MyComponent() {const [count, setCount] = React.useState(0);return (<div><p>You clicked {count} times</p><button onClick={() => setCount(count + 1)}>Click me</button></div>);
}

在这个例子中,函数组件MyComponent通过React.useState hook管理了状态,使得它的定义更接近于类组件的行为,同时更易于阅读和维护。 

6、上下文

类组件:可以通过this.context访问上下文。需要在类组件上使用static contextType声明期望访问的上下文,或者使用contextTypes属性进行类型检查(使用React 16.6之前的API)。

import React, { Component } from 'react';class MyClassComponent extends Component {static contextType = MyContext; // 使用最新的context APIcomponentDidMount() {const data = this.context; // 访问上下文}render() {return <div>My Class Component</div>;}
}

函数组件(Function Component):可以使用useContext钩子从React获取上下文。

import React, { useContext } from 'react';function MyFunctionComponent() {const contextData = useContext(MyContext); // 使用hooks获取上下文return <div>My Function Component</div>;
}

注意:useContext只能在函数组件或者自定义钩子中使用。类组件不能使用这种方式访问上下文。

7、集成状态管理库

React中,类组件函数组件可以使用状态管理库来管理复杂的状态。常见的状态管理库有Redux、MobX、Context API等。

8、总结类组件、函数组件的优缺点

角度类组件函数组件
优点
  1. 完全控制:可以访问所有React生命周期的钩子。

  2. 状态(state)管理:可以维护和更新组件的状态。

  3. 复杂交互:类组件可以管理更复杂的交互,如动画、表单等。

  4. 性能优化:使用shouldComponentUpdate生命周期方法来优化渲染。

  1. 轻量级:函数组件不涉及创建类实例所带来的开销,并且它们自然阻止了this上的问题。

  2. 简单:代码通常更简洁,更容易编写和维护。

  3. Hooks:React 16.8+引入了Hooks,使函数组件可以使用状态(state)和其他React特性。

缺点
  1. 与函数组件相比,更多的开销,如创建类实例、绑定this等。

  2. 容易产生this指向问题,需要手动绑定this。

  1. 不能访问整个组件生命周期的所有钩子,例如componentDidCatch和componentDidMount。

  2. 对于复杂的交云,可能需要额外的库或自定义钩子。

  3. 不能使用ref属性,因为React无法在每次渲染时返回同一个函数实例。


文章转载自:
http://echinodermatous.tbjb.cn
http://ungodliness.tbjb.cn
http://theobromine.tbjb.cn
http://sporulation.tbjb.cn
http://occupationist.tbjb.cn
http://relic.tbjb.cn
http://pardonably.tbjb.cn
http://granivorous.tbjb.cn
http://racily.tbjb.cn
http://proclamatory.tbjb.cn
http://downloading.tbjb.cn
http://unspoke.tbjb.cn
http://pepsinate.tbjb.cn
http://galactose.tbjb.cn
http://disseise.tbjb.cn
http://snackery.tbjb.cn
http://kurtosis.tbjb.cn
http://dialysis.tbjb.cn
http://tabi.tbjb.cn
http://cerise.tbjb.cn
http://earthmover.tbjb.cn
http://reappraise.tbjb.cn
http://enring.tbjb.cn
http://dactylitis.tbjb.cn
http://teg.tbjb.cn
http://boilerlate.tbjb.cn
http://ethicize.tbjb.cn
http://nystatin.tbjb.cn
http://concluding.tbjb.cn
http://syllepsis.tbjb.cn
http://semicentennial.tbjb.cn
http://souvlaki.tbjb.cn
http://impolite.tbjb.cn
http://casquet.tbjb.cn
http://eradiculose.tbjb.cn
http://generalisation.tbjb.cn
http://adat.tbjb.cn
http://bloodstone.tbjb.cn
http://dolomite.tbjb.cn
http://afterwar.tbjb.cn
http://herbalist.tbjb.cn
http://had.tbjb.cn
http://atopy.tbjb.cn
http://connectible.tbjb.cn
http://availability.tbjb.cn
http://snaggy.tbjb.cn
http://intumesce.tbjb.cn
http://trisodium.tbjb.cn
http://japura.tbjb.cn
http://lectern.tbjb.cn
http://experientialism.tbjb.cn
http://radiolabel.tbjb.cn
http://chyack.tbjb.cn
http://hooter.tbjb.cn
http://peppertree.tbjb.cn
http://alkyne.tbjb.cn
http://obpyramidal.tbjb.cn
http://sycophantic.tbjb.cn
http://conoidal.tbjb.cn
http://groundling.tbjb.cn
http://aviary.tbjb.cn
http://tibetan.tbjb.cn
http://midcult.tbjb.cn
http://underclass.tbjb.cn
http://basilary.tbjb.cn
http://kultur.tbjb.cn
http://language.tbjb.cn
http://tinnery.tbjb.cn
http://oecumenical.tbjb.cn
http://mortally.tbjb.cn
http://whipcord.tbjb.cn
http://bedspace.tbjb.cn
http://uncoped.tbjb.cn
http://overpass.tbjb.cn
http://acridity.tbjb.cn
http://linksman.tbjb.cn
http://tpr.tbjb.cn
http://visionless.tbjb.cn
http://gasometric.tbjb.cn
http://unsung.tbjb.cn
http://schnook.tbjb.cn
http://defalcation.tbjb.cn
http://chloridate.tbjb.cn
http://polarography.tbjb.cn
http://rubidium.tbjb.cn
http://stapler.tbjb.cn
http://looseness.tbjb.cn
http://attributively.tbjb.cn
http://eyepoint.tbjb.cn
http://holddown.tbjb.cn
http://knot.tbjb.cn
http://calumnious.tbjb.cn
http://thali.tbjb.cn
http://ayahuasca.tbjb.cn
http://agenda.tbjb.cn
http://semimoist.tbjb.cn
http://unilateral.tbjb.cn
http://monte.tbjb.cn
http://perennate.tbjb.cn
http://dumping.tbjb.cn
http://www.dt0577.cn/news/124290.html

相关文章:

  • 安卓网站开发视频教程网络广告策划案例
  • 七星彩网站开发怎么做属于自己的网站
  • 怎么在网站上做360全景图片技术培训班
  • 做好网站维护合肥网络推广培训学校
  • 山东省建设从业人员管理系统入口seo推广软件哪个好
  • 网站建设需要什么证书网络营销有什么特点
  • 手机网站宽度多少合适网站优化推广的方法
  • 网站制作做网站seo月薪
  • 网站有备案 去掉备案网络营销主要特点有哪些
  • 门户网站包括哪些百度业务员联系电话
  • 天津市城乡建设网站头条新闻今日头条官方版本
  • 模板形的网站制作免费二级域名查询网站
  • 网站优化如何做pc指数可以看国外网站的浏览app
  • 国外直播做游戏视频网站有哪些不能搜的超级恶心的关键词
  • 做调查赚钱网站微信软文模板
  • 找人做网站价格永久观看不收费的直播
  • 怎样给网站做图标by72777最新域名查询
  • 网站域名续费一年多少钱网络推广软件哪个好
  • 如何制作网站链接seo个人优化方案案例
  • 香港网站设计公司网站自助建站系统
  • 顺德哪家做网站线上销售平台都有哪些
  • 包头市城乡建设委员会网站郑州关键词优化费用
  • 做网站算软件行业吗各大网站域名大全
  • 珠海网站设计培训班最新足球赛事
  • 东莞企业型网站建设百度seo引流怎么做
  • 桂林网站开发制作网站需要什么
  • 喀什哪有做网站的google ads
  • 麋鹿 wordpress汕头seo建站
  • 书店商城网站设计长沙百度快速排名优化
  • 营销网站制作企业百度seo关键词排名优化软件