简体   繁体   English

React / Redux 警报最佳实践

[英]React / Redux alerting best practices

I'm new to redux, and I'm rebuilding a fairly complex reactjs app using redux.我是 redux 的新手,我正在使用 redux 重建一个相当复杂的 reactjs 应用程序。

I thought it made sense to build a "feature" for notifications that would have a slice of state with like我认为为通知构建一个“功能”是有意义的,该通知将包含 state

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { RootState } from '../../app/store';

export interface NotificationState {
    show: boolean,
    status: 'info' | 'warn' | 'error' | 'success',
    title: string,
    message: string,
    position: 'dash' | 'popover',
}

const initialState: NotificationState = {
    show: false,
    status: 'info',
    title: '',
    message: '',
    position: 'popover',
};

export const notificationSlice = createSlice({
    name: 'notification',
    initialState,
    reducers: {
        show: (state, action: PayloadAction<NotificationState>) => {
            state = action.payload;
        },
        hide: (state) => {
            state.show = false;
        },
        toggle: (state) => {
            state.show = !state.show;
        },
    },
});

const { actions, reducer } = notificationSlice;
export const { show, hide, toggle } = actions;
export const selectNotification = (state: RootState) => state.notification;
export default reducer;    

that would control how the notice shows, what position, what alert colors are used, etc.这将控制通知的显示方式,使用什么 position,使用什么警报 colors 等。

However, now that I'm getting down to the implementation, I'm finding that I want to show notifications based on other features' state.但是,现在我开始着手实现,我发现我想根据其他功能的 state 显示通知。 For example, in my blog-posts feature, I fetch data from the server via a thunk and I'd like to set a notification based on the state of the thunk:例如,在我的blog-posts功能中,我通过thunk从服务器获取数据,我想根据 thunk 的 state 设置通知:

extraReducers: (builder) => {
    builder
        .addCase(fetchBlogPosts.fulfilled, (state, action) => {
            state.status = 'idle';
            state.entities = action.payload;
        })
        // hopefully this will apply to any failed / pending request
        .addMatcher(isRejectedAction, (state, action) => {
            state.error = action.error;
            // store.dispatch(show({
            //     show: true,
            //     status: 'error',
            //     title: 'Request Failed',
            //     message: action.error.message,
            //     position: 'popover',
            //     autoHide: false,
            //     confirm: false,
            // }));
        })
        .addMatcher(isPendingAction, (state, action) => {
            state.status = 'loading';
        })
}

The obvious problem is that you're not supposed to dispatch action from reducers.明显的问题是你不应该从 reducer 发送动作。 Is this just a bad idea in general, or is there a way to set the notification state from a thunk response?一般来说,这只是一个坏主意,还是有办法从 thunk 响应中设置notification state? Is there a "best practices" way to deal with this?是否有“最佳实践”方法来处理这个问题?

In this particular case, I think you need to flip the implementation approach.在这种特殊情况下,我认为您需要翻转实现方法。

You could have the notifications slice add an extraReducers case that listens for any "rejected" action, and show a notification based on that action.您可以让通知切片添加一个extraReducers案例,以侦听任何“拒绝”操作,并根据该操作显示通知。 Multiple reducers handling one action is a pattern we specifically encourage:多个 reducer 处理一个动作是我们特别鼓励的模式:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM