ZooWork Gitee 注册 / 登录 开始使用
ZooWork

产品

社区

资源

定价

注册 / 登录开始使用
🧸

Dva 重构 Zustand 专家

arvinxx · 内容更新 2023年9月10日 · v1.0.0

编程
你可以使用该 Agent 做什么?

This Agent specializes in transforming state management code written using the Dva framework into equivalent Zustand code, tailored for React ecosystem developers. Leveraging deep expertise in both Dva and Zustand, it automates the refactoring process, ensuring code consistency, improved maintainability, and modern best practices. Users can input their Dva-based state management snippets, and the Agent will generate clean, idiomatic Zustand code, including proper typings, hooks, and asynchronous data handling patterns. This tool is invaluable for frontend engineers aiming to migrate legacy Dva codebases to Zustand or adopt a simpler, more flexible state management approach without manual rewriting errors.

Agent 演示

🧸

你好!我是你的 Dva 到 Zustand 状态管理代码重构专家。无论是简单的计数器还是复杂的异步数据流,只需提供你的 Dva 代码,我将帮你快速准确地转换为现代 Zustand 代码,提升代码质量和开发效率。

请帮我把这段 Dva 状态管理代码转换成 Zustand 代码:

export default {
  namespace: 'counter',
  state: { count: 0 },
  reducers: {
    increment(state) {
      return { ...state, count: state.count + 1 };
    },
    decrement(state) {
      return { ...state, count: state.count - 1 };
    },
  },
  effects: {
    *incrementAsync(action, { call, put }) {
      yield call(delay, 1000);
      yield put({ type: 'increment' });
    },
  },
};
🧸

好的,以下是将该 Dva 代码重构为 Zustand 的示例:

import create from 'zustand';

interface CounterState {
  count: number;
  increment: () => void;
  decrement: () => void;
  incrementAsync: () => Promise<void>;
}

export const useCounter = create<CounterState>((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
  decrement: () => set((state) => ({ count: state.count - 1 })),
  incrementAsync: async () => {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    set((state) => ({ count: state.count + 1 }));
  },
}));

如果我的 Dva 代码中有异步请求,比如调用接口获取数据,该怎么转换?

🧸

你可以在 Zustand 中利用异步函数结合 React 的数据获取库(如 useSWR)或者直接在状态管理中使用异步逻辑。比如:

import create from 'zustand';
import useSWR from 'swr';

interface DataState {
  data: any[];
  loading: boolean;
  fetchData: () => void;
}

export const useDataStore = create<DataState>((set) => ({
  data: [],
  loading: false,
  fetchData: async () => {
    set({ loading: true });
    try {
      const response = await fetch('/api/data');
      const result = await response.json();
      set({ data: result, loading: false });
    } catch {
      set({ loading: false });
    }
  },
}));

这样可以让你在 Zustand 中管理异步请求状态和数据。

请帮我把这段更复杂的 Dva 代码重构成 Zustand:

export default {
  namespace: 'todo',
  state: { list: [], loading: false },
  effects: {
    *fetchTodos(_, { call, put }) {
      yield put({ type: 'setLoading', payload: true });
      const data = yield call(fetchTodosApi);
      yield put({ type: 'saveTodos', payload: data });
      yield put({ type: 'setLoading', payload: false });
    },
  },
  reducers: {
    saveTodos(state, { payload }) {
      return { ...state, list: payload };
    },
    setLoading(state, { payload }) {
      return { ...state, loading: payload };
    },
  },
};
🧸

这是对应的 Zustand 重构代码示例:

import create from 'zustand';

interface TodoState {
  list: any[];
  loading: boolean;
  fetchTodos: () => Promise<void>;
}

export const useTodoStore = create<TodoState>((set) => ({
  list: [],
  loading: false,
  fetchTodos: async () => {
    set({ loading: true });
    try {
      const response = await fetch('/api/todos');
      const data = await response.json();
      set({ list: data });
    } catch (error) {
      console.error(error);
    } finally {
      set({ loading: false });
    }
  },
}));
ZooWork

Your Chief Agent Operator

立即开始使用 ZooWork,让 Agent 成为你的工作伙伴。

免费开始使用下载
一键运行任何 Agent