# pinia-for-react **Repository Path**: zlmcode/pinia-for-react ## Basic Information - **Project Name**: pinia-for-react - **Description**: 一个跟vue pinia类似的react状态管理 - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-12 - **Last Updated**: 2022-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pinia-for-react ``` 一个跟vue pinia类似的react状态管理 ``` ## 1.特点 ``` 1.可以直接在组件外部修改状态(解决目前大多数状态库不能在外部使用的问题) 2.状态分类管理(按模块拆分) 3.天然异步支持 4.开箱即用 无需provider根组件包裹 5.友好的typescript提示 ``` ## 2.使用 ``` npm i pinia-for-react or yarn add pinia-for-react ``` ## 3.示例代码 ### 1.全局普通store ```tsx import {defineStore} from "pinia-for-react"; const userStore = defineStore({ state() { return { username: 'aaa', password: '1111', password1: '1111', } }, actions: { setUserInfo(userInfo) { const state = this.$getState(); //可以访问内部提供的方法 this.$setState({ ...state, ...userInfo }) }, //异步调用 async syncUserInfo() { await new Promise(resolve => setTimeout(resolve, 2000)); this.$patch({ username: 'syncUserInfoaaa', password: 'syncUserInfo1111', password1: 'syncUserInfo1111', }) }, reset() { this.$reset() } } }); //满足hook以use开头规范 const useUserStore = userStore; //外部更新状态 setInterval(() => { userStore.setUserInfo({username: '我是来自组件外部更改的状态', username1: new Date().getTime()}) }, 3000) const ComponentA = () => { // 直接调用返回值 const [{username}] = useUserStore(); return (
{username}
ComponentA
) } const ComponentB = () => { //也可以直接使用useStore方法hook const [userInfo, actions] = userStore.useStore(); return (
{JSON.stringify(userInfo)}
) } const ComponentC = () => { const [userInfo, actions] = userStore.useStore(); return (
{JSON.stringify(userInfo)}
) } export default () => { return (
Hello world! {/*也可以直接调用返回的userStore变量的方法*/} {/*也可以直接调用返回的userStore变量的方法*/}
) } ``` ### 2.全局代理store ```tsx import {defineProxyStore} from "pinia-for-react"; const useUserProxyStore = defineProxyStore({ state() { return { username: 'aaa', password: '1111', password1: '1111', } }, actions: { setUserInfo(userInfo) { Object.assign(this.state,userInfo) }, async syncUserInfo (){ await new Promise(resolve=>setTimeout(resolve,2000)); Object.assign(this.state,{ username: 'syncUserInfoaaa', password: 'syncUserInfo1111', password1: 'syncUserInfo1111', }) }, reset() { this.$reset() } } }); //外部调用 useUserProxyStore.state.username = 'xxxxxx' export default useUserProxyStore ``` ```tsx import React from 'react' import useUserProxyStore from '/@/stores/user-proxy' import {useNavigate} from "react-router-dom"; import {Button, Card, Space} from 'antd' import './index.css'; const ComponentA = () => { //解构后set值无效,但任然可以拿到最新的store const [{username}, {setUserInfo}] = useUserProxyStore(); console.log('刷新ComponentA') return (
{username}
) } const ComponentB = () => { const [userInfo, {setUserInfo}] = useUserProxyStore.useStore(); console.log('刷新ComponentB') return (
{JSON.stringify(userInfo)}
) } let count = 0; const ComponentC = () => { const [userInfo, actions] = useUserProxyStore.useStore(); console.log('刷新ComponentC'+(++count)) return (
{JSON.stringify(userInfo)}
) } export default () => { const navigate = useNavigate(); const [state, actions] = useUserProxyStore.useStore(); const continuityUpdate = async ()=>{ for (let i = 0; i < 100; i++) { await new Promise((resolve)=>setTimeout(resolve,50)) useUserProxyStore.$patch({ username: Math.random()+'', }) } } return (

Index Page

) } ``` ## 4.运行示例 #### 1.安装依赖 `yarn & npm run install` #### 2.运行web或者taro `yarn run start:web` or `yarn run start:taro`