nell04 2025-01-11 19:03 采纳率: 9.1%
浏览 29

zustand如何进行监听属性的变化?

zustand如何进行监听属性的变化?
比如我有:Slice如下:

export interface SectionSlice {
  Sections: Section[],
  selectedSection: Section | undefined,
  isLoading: boolean,

  selectSection: (SectionID: number) => void,
  fetchSections: () => Promise<void>,
  addSection: (Section:Section) => Promise<void>,
  updateSection: (Section:Section) => Promise<void>,
  deleteSection: (id: number) => Promise<void>,

  SectionMods: SectionMod[],
  selectedSectionMod: SectionMod | undefined,
  selectSectionMod: (SectionModId: number) => Promise<void>,
  fetchSectionMods: (SectionId: number) => Promise<void>,
}

//type SliceType = StateCreator<SectionSlice, [], [], SectionSlice>

export const createSectionSlice: StateCreator<SectionSlice> = (set, get) => ({

  // 1.状态
  Sections: [] as Section[],
  selectedSection: undefined as Section | undefined,

  SectionMods: [] as SectionMod[],
  selectedSectionMod: undefined as SectionMod | undefined,

  // 2.操作状态的actions

  isLoading: false, // 是否正在操作
  fetchSections: async() => {
    try {
      set({ isLoading: true })
      const Sections: Section[] = await getSections()

      set({ Sections: Sections })
    } catch (error) {

    } finally {
      set({ isLoading: false })
    }
  },

  // 选择项目
  selectSection: (SectionID: number) => {

    // 基于SectionID获取Section
    const { Sections } = get()
    const filteredSection = (Sections as Section[]).filter((Section: Section) => Section.id === SectionID);



    set({ selectedSection: filteredSection[0] })
  },

  addSection: async(Section:Section) => {
    try {
      set({ isLoading: true })
      await addSection(Section)
    } catch (error) {

    } finally {
      const { fetchSections } = get(); // 通过get获取当前状态里的fetchSections方法
      fetchSections()
      set({ isLoading: false })
    }
  },

  updateSection: async(Section: Section) => {
    try {
      set({ isLoading: true })
      await updateSection(Section)
    } catch (error) {

    } finally {
      const { fetchSections } = get(); // 通过get获取当前状态里的fetchSections方法
      fetchSections()
      set({ isLoading: false })
    }
  },

  deleteSection: async(id: number) => {
    try {
      set({ isLoading: true })
      await deleteSection(id)
    } catch (error) {

    } finally {
      const { fetchSections } = get(); // 通过get获取当前状态里的fetchSections方法
      fetchSections()
      set({ isLoading: false })
    }
  },

  selectSectionMod: async(SectionModId: number) => {

    // 基于SectionID获取Section
    const { SectionMods } = get()
    const filteredSectionMod = (SectionMods as SectionMod[]).filter((SectionMod: SectionMod) => SectionMod.id === SectionModId);

    set({selectedSectionMod: filteredSectionMod[0]})
  },

  fetchSectionMods: async(SectionId: number) => {

    try {
      set({ isLoading: true })

      const SectionMods: SectionMod[] = await getSectionMods(SectionId)
      set({ SectionMods: SectionMods })

      const { selectedSectionMod } = get()
      if( !selectedSectionMod ) {

        const mainSectionMods: SectionMod[] = SectionMods.filter((SectionMod) => SectionMod.isMain === true);
        set({ selectedSectionMod:  mainSectionMods[0]})
      }

    } catch (error) {

    } finally {
      set({ isLoading: false })
    }

  },

})

我想要在Store内(可以是Slice中)中监听:selectedSection 变化(比如:我想要知道selectedSection改变之后,立即就触发方法,在方法里面操作),在监听的事件里面在做其他的操作。

  • 写回答

4条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2025-01-11 19:04
    关注
    让【道友老李】来帮你解答,本回答参考gpt编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    要监听属性的变化,可以使用React的useEffect钩子函数来实现。在useEffect中,可以传入一个函数,该函数用来处理属性变化时的逻辑。下面是使用useEffect监听selectedSection属性变化的示例:
    import React, { useEffect } from 'react';
    const Component = ({ slice }) => {
      const { selectedSection } = slice;
      useEffect(() => {
        console.log('selectedSection changed:', selectedSection);
        // 在此处可以编写处理selectedSection属性变化的逻辑
      }, [selectedSection]);
      return (
        // 组件的 JSX
      );
    };
    

    在上面的示例中,useEffect监听selectedSection属性的变化。每当selectedSection属性值发生改变时,useEffect中的回调函数会被调用,从而实现属性变化的监听。 另外,需要注意的是,要确保将selectedSection属性传递给Component组件,以便在组件中访问和监听该属性的变化。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月11日