调度后,组件中的状态值将更新

发布于 2025-01-27 09:51:19 字数 1535 浏览 2 评论 0原文

我在一个组件中具有一个函数,该功能将条目添加到我的状态1(在REDUX中通过RTK)。

API向我返回了此新条目的自动生成的ID,然后将其添加到状态1。

然后,我需要使用上面提到的状态1条目在第二个状态2(链接表)中创建一个条目(链接表)。

我的问题是,我的状态1在组件中没有直接更新,并且仅在完成整个功能后才进行更新,因此我无法在正确的时刻访问状态1的新条目的ID。

如何访问此新值,尤其是组件功能中的ID?

状态1

const spectacles = useAppSelector(spectacleSelector.selectAll);

函数在状态中添加条目1

const handleCreateClick = () => {
    MySwal.fire({
// not needed code
    }).then((result) => {
        if (result.isConfirmed) {
            dispatch(createSpectacleAsync(result.value!))
                .then(() => dispatch(createSiteDeTirAsync({ id_Spe: spectacles[spectacles.length - 1]?.id!, code: "C" })))
                .then(() => navigate(`/spectacles/${spectacles[spectacles.length - 1]?.id!}`));
        }
    });
};

async thunk函数以在状态中创建条目1

export const createSpectacleAsync = createAsyncThunk<Spectacle, string>(
    'artifices/createSpectacleAsync',
    async (title: any, thunkAPI) => {
        try {
            return await agent.Spectacle.createSpectacle({ Titre: title });
        } catch (error: any) {
            return thunkAPI.rejectWithValue({error: error.data})
        }
    }
)

extruareducer来管理新的API返回的条目

builder.addCase(createSpectacleAsync.fulfilled, (state, action) => {
    spectacleAdapter.addOne(state, action.payload);
    state.status = 'idle';
});

I have a function in a component that adds an entry to my state 1 (in Redux via RTK).

The api returns me the auto-generated id of this new entry and I add it to state 1.

I then need to create an entry in a second state 2 (linked table), using the id of the state 1 entry mentioned above.

My problem is that my state 1 is not directly updated in the component and it is only updated after the entire function has been completed so I don't have access to the id of the new entry of the state 1 at the right moment.

How can I access this new value and especially the id in the function of the component?

State 1

const spectacles = useAppSelector(spectacleSelector.selectAll);

Function to add an entry in the state 1

const handleCreateClick = () => {
    MySwal.fire({
// not needed code
    }).then((result) => {
        if (result.isConfirmed) {
            dispatch(createSpectacleAsync(result.value!))
                .then(() => dispatch(createSiteDeTirAsync({ id_Spe: spectacles[spectacles.length - 1]?.id!, code: "C" })))
                .then(() => navigate(`/spectacles/${spectacles[spectacles.length - 1]?.id!}`));
        }
    });
};

Async thunk function to create the entry in the state1

export const createSpectacleAsync = createAsyncThunk<Spectacle, string>(
    'artifices/createSpectacleAsync',
    async (title: any, thunkAPI) => {
        try {
            return await agent.Spectacle.createSpectacle({ Titre: title });
        } catch (error: any) {
            return thunkAPI.rejectWithValue({error: error.data})
        }
    }
)

ExtraReducer to manage the new entry returned by the API

builder.addCase(createSpectacleAsync.fulfilled, (state, action) => {
    spectacleAdapter.addOne(state, action.payload);
    state.status = 'idle';
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文