Last active
February 20, 2022 07:55
-
-
Save wtshek/81111cdd73f0d59eba6073f2564a6384 to your computer and use it in GitHub Desktop.
setup #redux-thunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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