I have followed this tutorial and I think I have done everything as required however I still have this warning:
Unhandled promise rejection: Error: Actions must be plain objects. Instead, the actual type was: 'Promise'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.
My code:
- Inside folder
storethis is myindex.jsfile:
import { createStore, applyMiddleware } from "redux";
import thunkMiddleware from "redux-thunk";
import { composeWithDevTools } from "redux-devtools-extension";
import rootReducer from "../reducers/index";
const composedEnhancer = composeWithDevTools(applyMiddleware(thunkMiddleware));
const store = createStore(rootReducer, composedEnhancer);
export default store;
Inside my App.js file:
...
import store from "./redux/store/index";
...
return (
<Provider store={store}>
<NavigationContainer>
<MainNavigator />
</NavigationContainer>
</Provider>
);
Inside my MainNavigator file:
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import {someAsyncFunctionFromRedux} from "../../redux/actions/index";
...
const MainComponent = (props) => {
...
useEffect(() => {
async function f() {
await props.someAsyncFunctionFromRedux();
}
f();
}, []);
Inside my actions/index.js
export async function someAsyncFunction() {
return async (dispatch) => {
await firebase
.firestore()
.collection("Users")
.get()
.then( (snapshot) => {
let users = [];
snapshot.docs.map((doc) => {
const data = doc.data();
const id = doc.id;
users.push({id, ...data})
})
dispatch({ type: USERS_STATE_CHANGE, users });
});
};
}
awaita promise-ified function. Try removing either theawaitor the.then()async- are working fine? maybe I miss something but to me it seems likeredux-thunkis not enabled to the store for some reasonconnectmethod method, but theuseDispatchhook instead.connectis only a legacy compatibility layer for legacy class components. Generally though, keep in mind that, as this tutorial states mutliple times throughout, it only shows Redux internals, not how you should write Redux nowadays. For that, please follow the "Redux essentials" tutorial from the same site.