Created
January 26, 2020 18:48
-
-
Save DuudeXX8/8815991727f683eb9540f47e0f74c4c9 to your computer and use it in GitHub Desktop.
Redux saga fetch example
This file contains 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
export const PUT_DATA = "PUT_DATA"; | |
export const LOAD_DATA = "LOAD_DATA"; | |
export const putData = dataFromServer => { | |
return { | |
type:PUT_DATA, | |
payload:dataFromServer | |
} | |
} | |
export const loadData = () => { | |
return { | |
type: LOAD_DATA, | |
} | |
} |
This file contains 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 { Connected } from "./Connected"; | |
function App() { | |
return ( | |
<div className="App"> | |
<Connected /> | |
</div> | |
); | |
} | |
export default App; |
This file contains 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 { useDispatch } from "react-redux"; | |
import { putData, loadData } from "./actions"; | |
export const Connected = _ => { | |
const dispatch = useDispatch(); | |
const click = _ => dispatch(loadData()); | |
return ( | |
<div> | |
<button onClick={click}>Click me</button> | |
</div> | |
) | |
} |
This file contains 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 App from './App'; | |
import { Provider } from "react-redux"; | |
import { createStore, applyMiddleware } from "redux"; | |
import * as serviceWorker from './serviceWorker'; | |
import { reducer } from "./reducers"; | |
import logger from "redux-logger"; | |
import createSagaMiddleware from "redux-saga"; | |
import { watchLoadData } from './sagas'; | |
const sagaMiddleware = createSagaMiddleware(); | |
const store = createStore(reducer, applyMiddleware(logger, sagaMiddleware)); | |
sagaMiddleware.run(watchLoadData); | |
ReactDOM.render( | |
<Provider {...{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(); |
This file contains 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 { PUT_DATA } from "./actions"; | |
const initialState = { | |
data: {} | |
} | |
export const reducer = (state = initialState, action) => { | |
if(action.type === PUT_DATA){ | |
return { | |
...state, | |
data: action.payload | |
} | |
} | |
} |
This file contains 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 { takeEvery,call, put } from "redux-saga/effects"; | |
import { LOAD_DATA, putData } from "./actions"; | |
function fetchdata(){ | |
return fetch('https://jsonplaceholder.typicode.com/todos/1') | |
.then(response => response.json()); | |
} | |
function* workerLoadData(){ | |
const res = yield call(fetchdata); | |
yield put(putData(res)); | |
} | |
export function* watchLoadData(){ | |
yield takeEvery(LOAD_DATA, workerLoadData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment