React 第三方状态管理库相关 -- Jotai 篇
一、Jotai
首先安装依赖:
npm install jotai
示例代码:
// src/store/index.js
import { countStore } from "./counterStore";
export { countStore };
// src/store/counterStore.js
import { atom } from 'jotai';
export const countStore = atom(0);
// src/App.jsx
import React from 'react';
import { useAtom } from 'jotai';
import { countStore } from './store';
const UserProfile = () => {
const [count, setCount] = useAtom(countStore);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count+1)}>Increment</button>
</div>
);
};
export default UserProfile;