您好,欢迎访问一九零五行业门户网

react用什么管理状态

react管理状态的工具:1、利用hooks进行状态管理;2、利用redux进行状态管理,这种方式的配套工具比较齐全,可以自定义各种中间件;3、利用mobx进行状态管理,它通过透明的函数响应式编程使得状态管理变得简单和可扩展。
本教程操作环境:windows7系统、react17.0.1版、dell g3电脑。
什么是 状态?jquery 时代,js 代码中混杂 dom 结构,各个流程庞杂交织时,就形成面条式代码,当使用发布订阅模型时,调试会一团乱麻。
jquery 是针对 过程 的命令式编程,而那么多命令,最终都是为了更新 ui 中的 数据,为什么不直接去改数据呢?
北京 → 上海,把 city=北京 变为 city=上海 就行。不管飞机火车步行抛锚,也不管路上会不会遇到王宝强,
现代前端框架的意义,就是问题解决思路的革新,把对 过程 的各种命令,变为了对 状态 的描述。
什么是状态?状态就是 ui 中的动态数据。
react 中的状态2013 年 5 月 react 诞生。但 2015 年之前,大概都是 jquery 的天下。2015 年 3 月 react 0.13.0 发布,带来了 class 组件写法。
在 react class 组件时代,状态就是 this.state,使用 this.setstate 更新。
为避免一团乱麻,react 引入了 组件 和 单向数据流 的理念。有了状态与组件,自然就有了状态在组件间的传递,一般称为 通信。
父子通信较简单,而深层级、远距离组件的通信,则依赖于 状态提升 + props 层层传递。
于是,react 引入了 context,一个用于解决组件 跨级 通信的官方方案。
但 context 其实相当于 状态提升,并没有额外的性能优化,且写起来比较啰嗦。
为优化性能,一般会添加多个 context,写起来就更啰嗦。在项目没那么复杂时,还不如层层传递简单。
什么是 状态管理?实用主义来说,状态管理 就是为了解决组件间的 跨级 通信。
当然,在使用状态管理库时,其会带来一些衍生的思维模式,比如如何组织 state,如何拆分公共逻辑、业务逻辑、组件逻辑等,但归根结底,这些都不是核心缘由。
核心就是为了解决实际问题 —— 为了通信。其它的各种概念与哲学,都不是必要的。
context 没那么好用,react 官方也没什么最佳实践,于是一个个社区库就诞生了。
react中的状态管理方式目前比较常用的状态管理方式有hooks、redux、mobx三种,下面我将详细介绍一下这三类的使用方法以及分析各自的优缺点,以供各位进行参考。
hooks状态管理用hooks进行状态管理主要有两种方式:
usecontext+usereducerusestate+useeffect
usecontext+usereducer
使用方法
1.创建store和reducer以及全局context
src/store/reducer.ts
import react from "react";// 初始状态export const state = { count: 0, name: "ry",};// reducer 用于修改状态export const reducer = (state, action) => { const { type, payload } = action; switch (type) { case "modifycount": return { ...state, count: payload, }; case "modifyname": return { ...state, name: payload, }; default: { return state; } }};export const globalcontext = react.createcontext(null);
2.根组件通过 provider 注入 context
src/app.tsx
import react, { usereducer } from "react";import './index.less'import { state as initstate, reducer, globalcontext} from './store/reducer'import count from './components/count'import name from './components/name'export default function () { const [state, dispatch] = usereducer(reducer, initstate); return ( <div> <globalcontext.provider value={{state, dispatch}}> <count /> <name /> </globalcontext.provider> </div> )}
3.在组件中使用
src/components/count/index.tsx
import { globalcontext } from "@/store/reducer";import react, { fc, usecontext } from "react";const count: fc = () => { const ctx = usecontext(globalcontext) return ( <div> <p>count:{ctx.state.count}</p> <button onclick={() => ctx.dispatch({ type: "modifycount", payload: ctx.state.count+1 })}>+1</button> </div> );};export default count;
src/components/name/index.tsx
import { globalcontext } from "@/store/reducer";import react, { fc, usecontext } from "react";const name: fc = () => { const ctx = usecontext(globalcontext) console.log("namererendered") return ( <div> <p>name:{ctx.state.name}</p> </div> );};export default name;
usestate+useeffect
使用方法
1.创建state和reducer
src/global-states.ts
// 初始statelet globalstate: globalstates = { count: 0, name: 'ry'}// reducerexport const modifyglobalstates = ( operation: globalstatesmodificationtype, payload: any) => { switch (operation) { case globalstatesmodificationtype.modify_count: globalstate = object.assign({}, globalstate, { count: payload }) break case globalstatesmodificationtype.modify_name: globalstate = object.assign({}, globalstate, { name: payload }) break } broadcast()}
src/global-states.type.ts
export interface globalstates { count: number; name: string;}export enum globalstatesmodificationtype { modify_count, modify_name}
2.写一个发布订阅模式,让组件订阅globalstate
src/global-states.ts
import { usestate, useeffect } from 'react'import { globalstates, globalstatesmodificationtype} from './global-states.type'let listeners = []let globalstate: globalstates = { count: 0, name: 'ry'}// 发布,所有订阅者收到消息,执行setstate重新渲染const broadcast = () => { listeners.foreach((listener) => { listener(globalstate) })}export const modifyglobalstates = ( operation: globalstatesmodificationtype, payload: any) => { switch (operation) { case globalstatesmodificationtype.modify_count: globalstate = object.assign({}, globalstate, { count: payload }) break case globalstatesmodificationtype.modify_name: globalstate = object.assign({}, globalstate, { name: payload }) break } // 状态改变即发布 broadcast()}// useeffect + usestate实现发布订阅export const useglobalstates = () => { const [value, newlistener] = usestate(globalstate) useeffect(() => { // newlistener是新的订阅者 listeners.push(newlistener) // 组件卸载取消订阅 return () => { listeners = listeners.filter((listener) => listener !== newlistener) } }) return value}
3.组件中使用
src/app.tsx
import react from 'react'import './index.less'import count from './components/count'import name from './components/name'export default function () { return ( <div> <count /> <name /> </div> )}
src/components/count/index.tsx
import react, { fc } from 'react'import { useglobalstates, modifyglobalstates } from '@/store/global-states'import { globalstatesmodificationtype } from '@/store/global-states.type'const count: fc = () => { // 调用useglobalstates()即订阅globalstates() const { count } = useglobalstates() return ( <div> <p>count:{count}</p> <button onclick={() => modifyglobalstates( globalstatesmodificationtype.modify_count, count + 1 ) } > +1 </button> </div> )}export default count
src/components/name/index.tsx
import react, { fc } from 'react'import { useglobalstates } from '@/store/global-states'const count: fc = () => { const { name } = useglobalstates() console.log('namererendered') return ( <div> <p>name:{name}</p> </div> )}export default count
优缺点分析
由于以上两种都是采用hooks进行状态管理,这里统一进行分析
优点
代码比较简洁,如果你的项目比较简单,只有少部分状态需要提升到全局,大部分组件依旧通过本地状态来进行管理。这时,使用 hookst进行状态管理就挺不错的。杀鸡焉用牛刀。
缺点
两种hooks管理方式都有一个很明显的缺点,会产生大量的无效rerender,如上例中的count和name组件,当state.count改变后,name组件也会rerender,尽管他没有使用到state.count。这在大型项目中无疑是效率比较低的。
redux状态管理
使用方法:
1.引入redux
yarn add redux react-redux @types/react-redux redux-thunk
2.新建reducer
在src/store/reducers文件夹下新建addreducer.ts(可建立多个reducer)
import * as types from '../action.types'import { anyaction } from 'redux'// 定义参数接口export interface addstate { count: number name: string}// 初始化statelet initialstate: addstate = { count: 0, name: 'ry'}// 返回一个reducerexport default (state: addstate = initialstate, action: anyaction): addstate => { switch (action.type) { case types.add: return { ...state, count: state.count + action.payload } default: return state }}
在src/stores文件夹下新建action.types.ts
主要用于声明action类型
export const add = 'add'export const delete = 'delete'
3.合并reducer
在src/store/reducers文件夹下新建index.ts
import { combinereducers, reducersmapobject, anyaction, reducer } from 'redux'import addreducer, { addstate } from './addreducer'// 如有多个reducer则合并reducers,模块化export interface combinedstate { addreducer: addstate}const reducers: reducersmapobject<combinedstate, anyaction> = { addreducer}const reducer: reducer<combinedstate, anyaction> = combinereducers(reducers)export default reducer
3.创建store
在src/stores文件夹下新建index.ts
import { createstore, applymiddleware, storeenhancer, storeenhancerstorecreator, store} from 'redux'import thunk from 'redux-thunk'import reducer from './reducers'// 生成store增强器const storeenhancer: storeenhancer = applymiddleware(thunk)const storeenhancerstorecreator: storeenhancerstorecreator = storeenhancer(createstore)const store: store = storeenhancerstorecreator(reducer)export default store
4.根组件通过 provider 注入 store
src/index.tsx(用provider将app.tsx包起来)
import react from 'react'import reactdom from 'react-dom'import app from './app'import { provider } from 'react-redux'import store from './store'reactdom.render( <provider store={store}> <app /> </provider>, document.getelementbyid('root'))
5.在组件中使用
src/somponents/count/index.tsx
import react, { fc } from 'react'import { connect } from 'react-redux'import { dispatch } from 'redux'import { addstate } from 'src/store/reducers/addreducer'import { combinedstate } from 'src/store/reducers'import * as types from '@/store/action.types'// 声明参数接口interface props { count: number add: (num: number) => void}// returntype获取函数返回值类型,&交叉类型(用于多类型合并)// type props = returntype<typeof mapstatetoprops> & returntype<typeof mapdispatchtoprops>const count: fc<props> = (props) => { const { count, add } = props return ( <div> <p>count: {count}</p> <button onclick={() => add(5)}>addcount</button> </div> )}// 这里相当于自己手动做了映射,只有这里映射到的属性变化,组件才会rerenderconst mapstatetoprops = (state: combinedstate) => ({ count: state.addreducer.count})const mapdispatchtoprops = (dispatch: dispatch) => { return { add(num: number = 1) { // payload为参数 dispatch({ type: types.add, payload: num }) } }}export default connect(mapstatetoprops, mapdispatchtoprops)(count)
src/somponents/name/index.tsx
import react, { fc } from 'react'import { connect } from 'react-redux'import { dispatch } from 'redux'import { addstate } from 'src/store/reducers/addreducer'import { combinedstate } from 'src/store/reducers'import * as types from '@/store/action.types'// 声明参数接口interface props { name: string}const name: fc<props> = (props) => { const { name } = props console.log('namererendered') return ( <div> <p>name: {name}</p> </div> )}// name变化组件才会rerenderconst mapstatetoprops = (state: combinedstate) => ({ name: state.addreducer.name})// addreducer内任意属性变化组件都会rerender// const mapstatetoprops = (state: combinedstate) => state.addreducerexport default connect(mapstatetoprops)(name)
优缺点分析
优点
组件会订阅store中具体的某个属性【mapstatetoprops手动完成】,只要当属性变化时,组件才会rerender,渲染效率较高流程规范,按照官方推荐的规范和结合团队风格打造一套属于自己的流程。配套工具比较齐全redux-thunk支持异步,redux-devtools支持调试可以自定义各种中间件
缺点
state+action+reducer的方式不太好理解,不太直观非常啰嗦,为了一个功能又要写reducer又要写action,还要写一个文件定义actiontype,显得很麻烦使用体感非常差,每个用到全局状态的组件都得写一个mapstatetoprops和mapdispatchtoprops,然后用connect包一层,我就简单用个状态而已,咋就这么复杂呢当然还有一堆的引入文件,100行的代码用了redux可以变成120行,不过换个角度来说这也算增加了自己的代码量好像除了复杂也没什么缺点了
mobx状态管理
mobx 是一个经过战火洗礼的库,它通过透明的函数响应式编程(transparently applying functional reactive programming - tfrp)使得状态管理变得简单和可扩展。
常规使用(mobx-react)
使用方法
1.引入mobx
yarn add mobx mobx-react -d
2.创建store
在/src/store目录下创建你要用到的store(在这里使用多个store进行演示)
例如:
store1.ts
import { observable, action, makeobservable } from 'mobx'class store1 { constructor() { makeobservable(this) //mobx6.0之后必须要加上这一句 } @observable count = 0 @observable name = 'ry' @action addcount = () => { this.count += 1 }}const store1 = new store1()export default store1
store2.ts
这里使用 makeautoobservable代替了makeobservable,这样就不用对每个state和action进行修饰了(两个方法都可,自行选择)
import { makeautoobservable } from 'mobx'class store2 { constructor() { // mobx6.0之后必须要加上这一句 makeautoobservable(this) } time = 11111111110}const store2 = new store2()export default store2
3.导出store
src/store/index.ts
import store1 from './store1'import store2 from './store2'export const store = { store1, store2 }
4.根组件通过 provider 注入 store
src/index.tsx(用provider将app.tsx包起来)
import react from 'react'import reactdom from 'react-dom'import app from './app'import store from './store'import { provider } from 'mobx-react'reactdom.render( <provider {...store}> <app /> </provider>, document.getelementbyid('root'))
5.在组件中使用
src/somponents/count/index.tsx
import react, { fc } from 'react'import { observer, inject } from 'mobx-react'// 类组件用装饰器注入,方法如下// @inject('store1')// @observerinterface props { store1?: any}const count: fc<props> = (props) => { const { count, addcount } = props.store1 return ( <div> <p>count: {count}</p> <button onclick={addcount}>addcount</button> </div> )}// 函数组件用hoc,方法如下(本文统一使用函数组件)export default inject('store1')(observer(count))
src/components/name/index.tsx
import react, { fc } from 'react'import { observer, inject } from 'mobx-react'interface props { store1?: any}const name: fc<props> = (props) => { const { name } = props.store1 console.log('namererendered') return ( <div> <p>name: {name}</p> </div> )}// 函数组件用hoc,方法如下(本文统一使用函数组件)export default inject('store1')(observer(name))
优缺点分析:
优点:
组件会自动订阅store中具体的某个属性,无需手动订阅噢!【下文会简单介绍下原理】只有当订阅的属性变化时,组件才会rerender,渲染效率较高一个store即写state,也写action,这种方式便于理解,并且代码量也会少一些
缺点:
当我们选择的技术栈是react+typescript+mobx时,这种使用方式有一个非常明显的缺点,引入的store必须要在props的type或interface定义过后才能使用(会增加不少代码量),而且还必须指定这个store为可选的,否则会报错(因为父组件其实没有传递这个prop给子组件),这样做还可能会致使对store取值时,提示可能为undefined,虽然能够用“!”排除undefined,可是这种作法并不优雅。
最佳实践(mobx+hooks)
使用方法
1.引入mobx
同上
2.创建store
同上
3.导出store(结合usecontext)
src/store/index.ts
import react from 'react'import store1 from './store1'import store2 from './store2'// 导出store1export const storecontext1 = react.createcontext(store1)export const usestore1 = () => react.usecontext(storecontext1)// 导出store2export const storecontext2 = react.createcontext(store2)export const usestore2 = () => react.usecontext(storecontext2)
4.在组件中使用
无需使用provider注入根组件
src/somponents/count/index.tsx
import react, { fc } from 'react'import { observer } from 'mobx-react'import { usestore1 } from '@/store/'// 类组件可用装饰器,方法如下// @observerconst count: fc = () => { const { count, addcount } = usestore1() return ( <div> <p>count: {count}</p> <button onclick={addcount}>addcount</button> </div> )}// 函数组件用hoc,方法如下(本文统一使用函数组件)export default observer(count)
src/components/name/index.tsx
import react, { fc } from 'react'import { observer } from 'mobx-react'import { usestore1 } from '@/store/'const name: fc = () => { const { name } = usestore1() console.log('namererendered') return ( <div> <p>name: {name}</p> </div> )}export default observer(name)
优缺点分析:
优点:学习成本少,基础知识非常简单,跟 vue 一样的核心原理,响应式编程。一个store即写state,也写action,这种方式便于理解组件会自动订阅store中具体的某个属性,只要当属性变化时,组件才会rerender,渲染效率较高成功避免了上一种使用方式的缺点,不用对使用的store进行interface或type声明!内置异步action操作方式代码量真的很少,使用很简单有没有,强烈推荐!
缺点:过于自由:mobx提供的约定及模版代码很少,这导致开发代码编写很自由,如果不做一些约定,比较容易导致团队代码风格不统一,团队建议启用严格模式!使用方式过于简单
mobx自动订阅实现原理
基本概念
observable //被观察者,状态observer //观察者,组件reaction //响应,是一类的特殊的 derivation,可以注册响应函数,使之在条件满足时自动执行。
建立依赖
我们给组件包的一层observer实现了这个功能
export default observer(name)
组件每次mount和update时都会执行一遍useobserver函数,useobserver函数中通过reaction.track进行依赖收集,将该组件加到该observable变量的依赖中(binddependencies)。
// fn = function () { return basecomponent(props, ref); export function useobserver(fn, basecomponentname) { ... var rendering; var exception; reaction.track(function () { try { rendering = fn(); } catch (e) { exception = e; } }); if (exception) { throw exception; // re-throw any exceptions caught during rendering } return rendering;}
reaction.track()
_proto.track = function track(fn) { // 开始收集 startbatch(); var result = trackderivedfunction(this, fn, undefined); // 结束收集 endbatch(); };
reaction.track里面的核心内容是trackderivedfunction
function trackderivedfunction<t>(derivation: iderivation, f: () => t, context: any) { ... let result // 执行回调f,触发了变量(即组件的参数)的 get,从而获取 dep【收集依赖】 if (globalstate.disableerrorboundaries === true) { result = f.call(context) } else { try { result = f.call(context) } catch (e) { result = new caughtexception(e) } } globalstate.trackingderivation = prevtracking // 给 observable 绑定 derivation binddependencies(derivation) ... return result}
触发依赖
observable(被观察者,状态)修改后,会调用它的set方法,然后再依次执行该observable之前收集的依赖函数,触发rerender。
组件更新
用组件更新来简单阐述总结一下:mobx的执行原理。
observer这个装饰器(也可以是hoc),对react组件的render方法进行track。
将render方法,加入到各个observable的依赖中。当observable发生变化,track方法就会执行。
track中,还是先进行依赖收集,调用forceupdate去更新组件,然后结束依赖收集。
每次都进行依赖收集的原因是,每次执行依赖可能会发生变化。
总结简单总结了一下目前较为常用的状态管理方式,我个人最喜欢的使用方式是mobx+hooks,简单轻量易上手。各位可以根据自己的需求选择适合自己项目的管理方式。
【相关推荐:redis视频教程】
以上就是react用什么管理状态的详细内容。
其它类似信息

推荐信息