Skip to content

Instantly share code, notes, and snippets.

@wtshek
Last active February 20, 2022 07:55
Show Gist options
  • Save wtshek/81111cdd73f0d59eba6073f2564a6384 to your computer and use it in GitHub Desktop.
Save wtshek/81111cdd73f0d59eba6073f2564a6384 to your computer and use it in GitHub Desktop.
setup #redux-thunk
import jsonPlaceholder from "../api/jsonPlaceholder";
export const fetchPosts = () => async dispatch => {
const response = await jsonPlaceholder.get("/posts");
dispatch({type:"FETCH_POSTS", payload: response })
}
//with getState
export const fetchPosts = () => {
const async (dispatch, getState) => {
const response = await jsonPlaceholder.get("/posts");
dispatch({type:"FETCH_POSTS", payload: response })
}
}
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from "react-redux";
import {createStore, applyMiddleware} from "redux"; //import another function
import thunk from 'redux-thunk'; //import statement
import rootReducers from "./redux/reducers"
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
const store = createStore(rootReducers, applyMiddleware(thunk)) //using middleware
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment