Skip to content

Commit

Permalink
feat: 🐻 添加useSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
iamzwq committed Nov 11, 2024
1 parent d79fd4b commit 69d2297
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/components/antd-config-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import type { PropsWithChildren } from "react";
import { legacyLogicalPropertiesTransformer, StyleProvider } from "@ant-design/cssinjs";
import { ConfigProvider, theme as antdTheme } from "antd";
import { useTheme } from "../theme-provider";
import { useSettingsStore } from "@/stores/settings";
import { useSelector, useSettingsStore } from "@/stores";

import zhCN from "antd/locale/zh_CN";
import "dayjs/locale/zh-cn";

export function AntdConfigProvider({ children }: PropsWithChildren) {
const { isDarkMode } = useTheme();
const { defaultAlgorithm, darkAlgorithm } = antdTheme;
const colorPrimary = useSettingsStore((state) => state.colorPrimary);
const { colorPrimary } = useSettingsStore(useSelector(["colorPrimary"]));
return (
<StyleProvider hashPriority="high" transformers={[legacyLogicalPropertiesTransformer]}>
<ConfigProvider
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/components/Sider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link, useLocation } from "react-router-dom";
import { BarChartOutlined, HomeOutlined, MenuOutlined, UserOutlined } from "@ant-design/icons";
import { Layout, Menu, type MenuProps } from "antd";
import { useTheme } from "@/components/theme-provider";
import { useSettingsStore } from "@/stores/settings";
import { useSelector, useSettingsStore } from "@/stores";

import ReactIcon from "@/assets/svg/react.svg?react";

Expand Down Expand Up @@ -72,7 +72,7 @@ export default function Sider() {
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
const [openKeys, setOpenKeys] = useState<string[]>([]);

const collapsed = useSettingsStore((state) => state.collapsed);
const { collapsed } = useSettingsStore(useSelector(["collapsed"]));

const { isDarkMode } = useTheme();

Expand Down
4 changes: 2 additions & 2 deletions src/layouts/components/custom-skin.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SkinOutlined } from "@ant-design/icons";
import { ColorPicker } from "antd";
import { setColorPrimary, useSettingsStore } from "@/stores/settings";
import { setColorPrimary, useSelector, useSettingsStore } from "@/stores";

export default function CustomSkin() {
const colorPrimary = useSettingsStore((state) => state.colorPrimary);
const { colorPrimary } = useSettingsStore(useSelector(["colorPrimary"]));
return (
<ColorPicker
showText
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Content from "./components/content";
import CustomSkin from "./components/custom-skin";
import Sider from "./components/sider";
import UserAvatar from "./components/user-avatar";
import { setCollapsed, useSettingsStore } from "@/stores/settings";
import { setCollapsed, useSelector, useSettingsStore } from "@/stores";

export default function MainLayout() {
const collapsed = useSettingsStore((state) => state.collapsed);
const { collapsed } = useSettingsStore(useSelector(["collapsed"]));

return (
<Layout>
Expand Down
12 changes: 12 additions & 0 deletions src/pages/landing/components/test-1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Button } from "antd";
import { useSelector, useTestStore } from "@/stores";

export function Test1() {
const { a, addA } = useTestStore(useSelector(["a", "addA"]));
return (
<div className="flex flex-col gap-4 items-center justify-center border border-teal-400 w-[300px] h-[200px] rounded-sm">
<Button onClick={addA}>test-1: {a}</Button>
<p>{Math.random()}</p>
</div>
);
}
12 changes: 12 additions & 0 deletions src/pages/landing/components/test-2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Button } from "antd";
import { useSelector, useTestStore } from "@/stores";

export function Test2() {
const { b, addB } = useTestStore(useSelector(["b", "addB"]));
return (
<div className="flex flex-col gap-4 items-center justify-center border border-teal-400 w-[300px] h-[200px] rounded-sm">
<Button onClick={addB}>test-2: {b}</Button>
<p>{Math.random()}</p>
</div>
);
}
6 changes: 6 additions & 0 deletions src/pages/landing/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Helmet } from "react-helmet-async";
import { Button, DatePicker, Flex, Typography } from "antd";
import { Test1 } from "./components/test-1";
import { Test2 } from "./components/test-2";

const { RangePicker } = DatePicker;

Expand All @@ -16,6 +18,10 @@ export default function LandingPage() {
message
</Button>
</Flex>
<Flex gap={16}>
<Test1 />
<Test2 />
</Flex>
<div className="h-screen" />
</>
);
Expand Down
3 changes: 1 addition & 2 deletions src/pages/login/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useNavigate } from "react-router-dom";
import { LockOutlined, UserOutlined } from "@ant-design/icons";
import { Button, Form, type FormProps, Input } from "antd";
import { useLogin } from "../api";
import { notification } from "@/utils";

type FieldType = {
username?: string;
Expand All @@ -19,7 +18,7 @@ export default function LoginForm() {
onLogin(values);
navigate("/landing");
setTimeout(() => {
notification.success({
window.$notification?.success({
message: "登录成功",
description: "欢迎回来",
});
Expand Down
3 changes: 3 additions & 0 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./selectors";
export * from "./settings";
export * from "./test";
5 changes: 5 additions & 0 deletions src/stores/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useShallow } from "zustand/shallow";
import { pick } from "@/utils";

export const useSelector = <T extends object, K extends keyof T>(fields: K[]) =>
useShallow((state: T) => pick(state, fields));
9 changes: 0 additions & 9 deletions src/stores/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { useShallow } from "zustand/react/shallow";

const initialState = {
colorPrimary: "#1DA57A",
Expand All @@ -11,14 +10,6 @@ export const useSettingsStore = create<typeof initialState>()(
persist(() => initialState, { name: "app-settings" }),
);

export const useShallowSettingsStore = () =>
useSettingsStore(
useShallow((state) => ({
colorPrimary: state.colorPrimary,
collapsed: state.collapsed,
})),
);

export function setColorPrimary(colorPrimary: string) {
useSettingsStore.setState({ colorPrimary });
}
Expand Down
17 changes: 17 additions & 0 deletions src/stores/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { create } from "zustand";

type State = {
a: number;
addA: () => void;
b: number;
addB: () => void;
};

export const useTestStore = create<State>()((set) => {
return {
a: 1,
addA: () => set((state) => ({ a: state.a + 1 })),
b: 2,
addB: () => set((state) => ({ b: state.b + 1 })),
};
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./array";
export * from "./date";
export * from "./debounce-throttle";
export * from "./is";
export * from "./object";
export * from "./tailwind";

0 comments on commit 69d2297

Please sign in to comment.