跳到主要内容
版本:Next

状态管理

1.jotai

import { atom, useAtom } from "jotai";

// Create your atoms and derivatives
const textAtom = atom("hello");
const uppercaseAtom = atom((get) => get(textAtom).toUpperCase());

// Use them anywhere in your app
const Input = () => {
const [text, setText] = useAtom(textAtom);
const handleChange = (e) => setText(e.target.value);
return <input value={text} onChange={handleChange} />;
};

const Uppercase = () => {
const [uppercase] = useAtom(uppercaseAtom);
return <div>Uppercase: {uppercase}</div>;
};

// Now you have the components
const App = () => {
return (
<>
<Input />
<Uppercase />
</>
);
};

2.Redux Toolkit

counterSlice.js
import { createSlice } from "@reduxjs/toolkit";

export const counterSlice = createSlice({
name: "counter",
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});

export const { increment, decrement, incrementByAmount } = counterSlice.actions;

// The function below is called a thunk and allows us to perform async logic. It
// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This
// will call the thunk with the `dispatch` function as the first argument. Async
// code can then be executed and other actions can be dispatched
export const incrementAsync = (amount) => (dispatch) => {
setTimeout(() => {
dispatch(incrementByAmount(amount));
}, 1000);
};

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectCount = (state) => state.counter.value;

export default counterSlice.reducer;
store
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "../features/counter/counterSlice";

export default configureStore({
reducer: {
counter: counterReducer,
},
});

3.mobx

import React from "react";
import ReactDOM from "react-dom";
import { makeAutoObservable } from "mobx";
import { observer } from "mobx-react";

// Model the application state.
class Timer {
secondsPassed = 0;

constructor() {
makeAutoObservable(this);
}

increase() {
this.secondsPassed += 1;
}

reset() {
this.secondsPassed = 0;
}
}

const myTimer = new Timer();

// Build a "user interface" that uses the observable state.
const TimerView = observer(({ timer }) => (
<button onClick={() => timer.reset()}>
Seconds passed: {timer.secondsPassed}
</button>
));

ReactDOM.render(<TimerView timer={myTimer} />, document.body);

// Update the 'Seconds passed: X' text every second.
setInterval(() => {
myTimer.increase();
}, 1000);

4.vuex

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
//数据,相当于data
state: {},
getters: {},
//里面定义方法,操作state方发
mutations: {},
// 操作异步操作mutation
actions: {},
modules: {},
});

5.pinia

6.xstate