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;
原文地址:https://blog.csdn.net/weixin_59685936/article/details/145172800
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!