-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using with Redux Toolkit #245
Comments
Same error using aliases, any fix? |
@princesust @pabloat81 aliases need to go first as they are intercepting and remapping fired actions to aliased ones. Then they remapped actions pass through middleware / reducer. If you put alias at the end, the remapped actions don't do anything cuz you are at the end of the middleware stack and then they jus go through the reducers |
Hi @walleXD, thanx for the response. I only have the code similar to the example of readme.md and get that error (any idea what i am doing wrong?): import {applyMiddleware ,createStore} from 'redux'; const aliases = { const store = createStore(rootReducer, applyMiddleware( wrapStore(store); |
Something i forgot to mention is that the alias gets executed aside the warning or error message. Regards. |
Getting the same error using RTK. The alias is executing correctly, so no problem there, only the error |
Found a solution... In the alias you need to return the action const aliases = {
['accounts/queryAccounts']: (action: PayloadAction<QueryPayload>) => {
// do stuff
return action; // Return this
},
}; |
Is there an example using webext-redux with RTK? import { initStore } from '@/store';
import { wrapStore } from 'webext-redux';
const store = initStore(); // this will use `configureStore` and `createSlice` to init
wrapStore(store); |
I use import {
configureStore,
getDefaultMiddleware,
combineReducers,
Action,
} from '@reduxjs/toolkit';
import { alias, wrapStore } from 'webext-redux';
import { ThunkAction, ThunkMiddleware } from 'redux-thunk';
import logger from 'redux-logger';
import reducerMap from './slices';
import aliasMap from './aliases';
const rootReducer = combineReducers(reducerMap);
export type BackgroundState = ReturnType<typeof rootReducer>;
const store = configureStore({
reducer: rootReducer,
middleware: [
alias(aliasMap),
...getDefaultMiddleware<BackgroundState>(),
process.env.NODE_ENV !== 'production' && logger,
].filter(Boolean) as ThunkMiddleware<BackgroundState>[],
});
const { dispatch } = store;
wrapStore(store);
export type BackgroundDispatch = typeof store.dispatch;
export type BackgroundThunk = ThunkAction<
void,
BackgroundState,
unknown,
Action<string>
>;
export { store };
export default dispatch; |
@tknickman Is there a way to use Redux DevTools for debugging state in |
@buryo not to my knowledge, but using things like |
Hey @tknickman, do you mind informing me/us of a couple more things. do you add API to the middleware and the reducer? const store = configureStore({
reducer: {
foo: fooReducer,
[api.reducerPath]: api.reducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(api.middleware).concat(logger),
});
wrapStore(store); do you call your API directly in the react component? const {
data,
error,
isLoading,
} = useGetDataQuery({
foo:"bar"
}); |
@seanwessmith it's been ages so I had to go dig up the code for this. I configured the API calls using aliases. In the store I setup the aliases as middleware: const store = configureStore({
reducer: rootReducer,
middleware: [
alias(aliasMap),
// ... other middleware
}); And then I have a bunch of aliases defined. Here's one item in the map as an example: 'alias/fetch-data': (
action: PayloadAction<{
tabId: number;
data: { version: string; name: string; env: string };
}>,
): BackgroundThunk => (dispatch: BackgroundDispatch): AnyAction => {
const { data, tabId } = action.payload;
// call async API and dispatch result to a slice (we'll call it `slice-x`)
fetchFromAPI({
dispatch,
...
});
}
return { type: 'alias/fetch-data' };
}, Then I use this in a component with: const dispatch = useDispatch();
// get the data that will store the result of the fetch
const data =
useSelector(state => get(state, ['slice-x', type, id])) ?? {};
// put this wherever you need it (event handler, effect, etc)
dispatch({
type: 'alias/fetch-data',
payload: {
...
},
}) That's how I did it 4 years ago (and haven't touched the code since). Hope that helps! |
Haha yea I know its been awhile, really appreciate the find and share. thanks a lot! |
@seanwessmith Did you get RTK-Query working with webext-redux? I am facing an issue where I get |
When I try to use redux toolkit, alias is dispatched and corresponding function is also called but after that showing an error
wrapStore.js:97 Error: Actions must be plain objects. Use custom middleware for async actions.
Below are full code
import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit";
import { wrapStore, alias } from "webext-redux";
import logger from "redux-logger";
import rootReducer from "./Reducers";
const aliases = {
"user-clicked-alias": () => {
console.log("user-clicked-alias");
},
};
const store = configureStore({
reducer: rootReducer,
middleware: [...getDefaultMiddleware(), logger, alias(aliases)],
});
wrapStore(store);
The text was updated successfully, but these errors were encountered: