比React—Redux和Redux好用100倍的原子级状态管理器,是我们公司自己封装的npm包,如果感兴趣可以研究研究,会大大提高开发效率

news/2024/7/15 19:46:32 标签: react.js, npm, javascript

简介

react-store

一. 安装

在package.json的dependencies中引入

二. 配置

2.1 在store/index.ts中引入

2.3 在顶层通过StoreProvider注入状态

三. 使用

3.1 在函数组件中使用,借助useModel

3.2 在class组件中使用,借助connectModel

3.3 在组件外使用, 借助getModel

简介

大家都遭受rudex的毒打吧,基础掌握不好,会出现很多问题,然而React封装的React-Redux也没有好哪去呀!

我们公司使用的这个是自己上传的npm包,基于react-contextreact原子化状态管理器,具有完整的ts类型推测。

react-store

基于react-contextreact原子化状态管理器,具有完整的ts类型推测。使用方法及其简单!

一. 安装

package.json的dependencies中引入

  {
    "react-store": "https://h5-fc-code.oss-cn-hangzhou.aliyuncs.com/package/react-store/react-store-1.1.0.tgz"
  }

然后执行安装依赖操作

  npm i react-store -S

二. 配置

2.1 在store/index.ts中引入

import { useState } from 'react'
​
/** 1. 引入react-store */
import createStore from 'react-store'
​
/** 2. 定义各个原子化状态 */
// user
const userModel = () => {
  const [ userInfo, setUserInfo ] = useState<{ name: string }>({ name: 'name' })
  return { userInfo, setUserInfo }
}
​
// other
const otherModel = () => {
  const [ other, setOther ] = useState<number>(20)
  return { other, setOther }
}
​
/** 3. 组合所有状态 */
const store = createStore(() => ({
  user: userModel(),
  other: otherModel(),
}))
​
/** 向外暴露useModel, StoreProvider, getModel, connectModel */
export const { useModel, StoreProvider, getModel, connectModel } = store

2.3 在顶层通过StoreProvider注入状态

// src/main.ts
import React from 'react'
import ReactDOM from 'react-dom'
import App from '@/App'
// 1. 引入StoreProvider
import { StoreProvider } from '@/store'
​
// 2. 使用StoreProvider包裹App组件
ReactDOM.render(
  <StoreProvider>
    <App />
  </StoreProvider>,
  document.getElementById('root')
)

三. 使用

3.1 在函数组件中使用,借助useModel

import React from 'react'
import { useModel } from '@/store'
​
function FunctionDemo() {
​
  /** 通过useModel取出user状态 */
  const { userInfo, setUserInfo } = useModel('user')
​
  /** 在点击事件中调用setUserInfo改变状态 */
  const onChangeUser = () => {
    setUserInfo({
      name: userInfo.name + '1',
    })
  }
​
  // 展示userInfo.name
  return (
    <button onClick={onChangeUser}>{userInfo.name}--改变user中的状态</button>
  )
}
​
export default FunctionDemo

3.2 在class组件中使用,借助connectModel

import React, { Component } from 'react'
import { connectModel } from '@/store'
​
// 定义class组件props
interface IClassDemoProps {
  setOther: React.Dispatch<React.SetStateAction<string>>
  other: number
  num: number
}
​
class ClassDemo extends Component<IClassDemoProps> {
​
  // 通过this.props获取到方法修改状态
  onChange = () => {
    this.props.setOther(this.props.other + 1)
  }
​
  render() {
    console.log(this.props.num)
    // 通过this.props获取到状态进行展示
    return <button onClick={this.onChange}>{this.props.other}</button>
  }
}
​
// 通过高阶组件connectModel把other状态中的属性和方法注入到类组件中
export default connectModel('other',state => ({
  other: state.other,
  setOther: state.setOther
}))(ClassDemo)

3.3 在组件外使用, 借助getModel

import { getModel } from '@/store'
​
export const onChangeUser = () => {
    // 通过getModel读取user状态,进行操作
  const user = getModel('user')
  user.setUserInfo({
    name: user.userInfo.name + '1'
  })
}
​
// 1秒后执行onChangeUser方法
setTimeout(onChangeUser, 1000)

大家可以测试下真的很好用,一个子爽!


http://www.niftyadmin.cn/n/11509.html

相关文章

Linux零基础从入门到精通,必学的55个指令合集【上篇】

Linux学习笔记 资料下载&#xff1a; 链接: https://pan.baidu.com/s/1UvwkJaEJO7W3sU5qkCgKzA?pwdfe2f提取码: fe2f 本篇文章主要适用0基础的读者&#xff0c;内容会比较通俗易懂&#xff0c;也会有详细的图解教程&#xff0c;以及运行后的返回结果。我本人在系统性的学习…

【计算机网络】HTTP/HTTPS协议基础知识汇总

目录 1.URL&#xff1a; 2.HTTP协议&#xff1a; 2.1抓包工具&#xff08;这里用fiddler&#xff09;&#xff1a; 2.2请求和响应的格式&#xff1a; 2.3方法的介绍&#xff1a; 2.4请求报头&#xff08;header&#xff09;&#xff1a; 2.5状态码&#xff1a; 2.6响应…

[附源码]java毕业设计幼儿园管理系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

OFD查验~看OFD 用超阅

看OFD 用超阅 OFD概念 OFD是开放版式文档&#xff08;Open Fixed-layout Document &#xff09;的英文缩写&#xff0c;是中国版式文档格式标准——《GB/T 33190-2016电子文件存储与交换格式-版式文档》。版式文档的代表就是我们工作和生活中非常熟悉的PDF文档&#xff0c;OF…

【每日一题Day38】LC809情感丰富的文字 | 双指针 模拟

情感丰富的文字【LC809】 Sometimes people repeat letters to represent extra feeling. For example: "hello" -> "heeellooo""hi" -> "hiiii" In these strings like "heeellooo", we have groups of adjacent let…

使用json、yaml、toml作为配置文件,你知道他们的区别吗

前言 配置文件&#xff0c;不言而喻&#xff0c;主要是我们进行项目和工程配置的文件。 如果是站在前端角度说的话&#xff0c;我们最常接触的就是 json以及 js类型的文件&#xff0c;这种形式的配置写法对前端非常友好&#xff0c;因为都是我们熟悉的 JS 对象结构&#xff0…

中石油测井-技术研发岗回顾

前提&#xff1a; 时间&#xff1a;2022年11月25日 结果&#xff1a;暂未可知 阶段&#xff1a;面试结束 等结果 整个过程中&#xff0c;注意查看官网 中石油招聘 投递之前关注一下基本要求&#xff08;学历-专业&#xff09; 招聘人数&#xff1a;应聘人数 &#xff08;通过能…

c#调用谷歌浏览器打开文件 系统找不到指定的文件

1、文件不存在 需要查询注册表文件安装路径&#xff0c;部分电脑会不存在&#xff0c;需要更新谷歌浏览器 SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe 不存在的更新谷歌浏览器即可。 string cmdPath GetChromePath("chrome.exe"); …