Skip to content

Instantly share code, notes, and snippets.

@takahiro-saeki
Last active February 15, 2018 06:52
Show Gist options
  • Save takahiro-saeki/043b686554425e0a5feeff13e75a0ce6 to your computer and use it in GitHub Desktop.
Save takahiro-saeki/043b686554425e0a5feeff13e75a0ce6 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
export default class SampleClass extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
//this is action, and you need to set up bindActionCreators if you wanna use action directory.
this.props.fetchDogApi()
}
render() {
return <div>something</div>
}
}
// constant
const FETCH_DOG_API = 'FETCH_DOG_API'
const SUCCESS_LOAD = 'SUCCESS_LOAD'
// action
const fetchDogApi = () => ({
type: FETCH_DOG_API
})
const successLoad = ({payload}) => ({
type: SUCCESS_LOAD,
payload
})
// reducer
const reducer = (state, action) => {
if(action.type === SUCCESS_LOAD) {
const param = {...state, ...action.payload}
return param,
}
return state
}
//saga.js
function* dogRequest() {
while(true) {
const action = yield take(FETCH_DOG_API);
const request = await fetch('PATH_TO_DOGAPI')
const json = await request.json()
put(successLoad(json))
}
}
function* rootSaga() {
yield fork(dogRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment