【发布时间】:2021-10-06 07:55:51
【问题描述】:
我在我的一个 reducer 中使用了一个事件监听器,我需要根据传入的“类型”触发一个动作。我可以监听事件,我可以看到有效负载,但我无法调度另一个动作。我唯一需要做的就是发送一个动作。我怎样才能做到这一点 ?我不能从减速器调度。我正在使用 redux-thunk。我尝试将它与中间件一起使用,但我无法成功。谢谢。
import { ADD_NOTIF, REMOVE_NOTIF, CLOSE_NOTIFS } from './actions';
import Notifications from "../util/Notifications";
import { addNotif } from "./actions"
const localStatus = JSON.parse(localStorage.getItem("notifStatus"));
const initialState = { notif: [], notifStatus: localStatus !== null ? localStatus : true }
const NotifReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_NOTIF:
return {
...state,
notif: action.payload,
};
case REMOVE_NOTIF:
return {
...state,
notif: state.notif.filter(i => i.id !== action.payload.id)
};
case CLOSE_NOTIFS:
localStorage.setItem("notifStatus", JSON.stringify(action.payload.status));
return {
...state,
notifStatus: action.payload.status,
};
case 'TWITTER_NET_TWEET':
return {
...state,
notifStatus: action.payload.tweet
}
case 'PHOTO_SAVE_ERROR':
// Here i have to dispatch addNotif
return {
...state,
}
default:
return state;
}
};
我合并reducers的文件;
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import appReducer from 'containers/Main/reducer';
import notifReducer from '../src/redux/notifreducer';
import GalleryReducer from '../src/redux/Gallery/gallery-reducer';
import PlaneModeReducer from '../src/redux/planeModereducer';
import ContactsReducer from '../src/redux/Contacts/contactsReducer';
import CallReducer from '../src/redux/CallModule/callReducer';
import BackgroundReducer from '../src/redux/Background/background-reducer';
import SoundsReducer from '../src/redux/Sounds/soundsReducer';
import TwitterReducer from '../src/redux/Twitter/twitter-reducer';
import SahibindenReducer from '../src/redux/Sahibinden/sahibinden-reducer';
import BadgeReducer from '../src/redux/BadgeNotifications/badgeReducer';
import NewsReducer from "../src/redux/News/newsReducer";
import NotesReducer from "../src/redux/Notes/notesReducer";
import MessagesReducer from "../src/redux/Messages/messagesReducer";
const reducers = combineReducers({
app: appReducer,
notif: notifReducer,
gallery: GalleryReducer,
planeMode: PlaneModeReducer,
contacts: ContactsReducer,
inCall: CallReducer,
background: BackgroundReducer,
sounds: SoundsReducer,
twitter: TwitterReducer,
sahibinden: SahibindenReducer,
openedApp: BadgeReducer,
news: NewsReducer,
messages: MessagesReducer,
notes: NotesReducer
});
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
reducers,
composeEnhancers(applyMiddleware(thunk))
);
export default store;
【问题讨论】:
标签: reactjs redux react-redux react-hooks