Skip to content

Instantly share code, notes, and snippets.

@benglass
Created January 17, 2018 22:19
Show Gist options
  • Save benglass/a8d0f05067010a7ca8e5715fe64606f8 to your computer and use it in GitHub Desktop.
Save benglass/a8d0f05067010a7ca8e5715fe64606f8 to your computer and use it in GitHub Desktop.
// Normal action creator
function selectChart(chartId) {
return {
type: 'SELEcT_CHART',
id: chartId
}
}
dispatch(selectChart(5))
// Thunk action creator (returns a function)
function loadChart(chartId) {
return function (dispatch) {
dispatch({ type: 'LOAD_CHART', chartId });
fetch(`/charts/${chartId}`)
.then(chart => dispatch({ type: 'LOAD_CHART_SUCCEEDED', chart: chart });
.catch(e => dispatch({ type: 'LOAD_CHART_FAILED', error: e });
}
}
function selectChart(chartId) {
return function (dispatch) {
dispatch({ type: 'SELECT_CHART', chartId });
dispatch(loadChart(chartId));
}
}
dispatch(selectChart(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment