π https://github.com/happypoulp/redux-tutorial/wiki
Tutorial 08 - dispatch async action 2
λ¨Όμ dispatch-async-action-1μμ νλ λΉλκΈ° action creatorλ₯Ό μ€νν΄λ³΄μ.
import { createStore, combineReducers } from 'redux'
var reducer = combineReducers({
speaker: function (state = {}, action) {
console.log('speaker κ° μ λ¬λ°μ μν', state, 'μ μ‘μ
', action)
switch (action.type) {
case 'SAY':
return {
...state,
message: action.message
}
default:
return state;
}
}
})
var store_0 = createStore(reducer)
var asyncSayActionCreator_1 = function (message) {
return function (dispatch) {
setTimeout(function () {
dispatch({
type: 'SAY',
message
})
}, 2000)
}
}
console.log("\n", 'Running our async action creator:', "\n")
store_0.dispatch(asyncSayActionCreator_1('Hi'))
// Output:
// ...
// /Users/classtar/Codes/redux-tutorial/node_modules/redux/node_modules/invariant/invariant.js:51
// throw error;
// ^
// Error: Invariant Violation: Actions must be plain objects. Use custom middleware for async actions.
// ...
μ°λ¦¬κ° λ§λ ν¨μλ reducer κ·Όμ²μλ λͺ»κ°λ€. κ·Έλ μ§λ§ μΉμ ν Reduxκ° μλ €μ£Όλ νμ 보면: βλΉλκΈ° μ‘μ μλ 컀μ€ν ν λ―Έλ€μ¨μ΄λ₯Ό μ¨λΌ.β. μ΄λλ‘ νλ©΄ λ κ² κ°μλ° βλ―Έλ€μ¨μ΄βκ° λμ§?
μ μμ¬μ μ’ λμ΄μ£Όμλ©΄, μ°λ¦¬κ° λ§λ action creator asyncSayActionCreator_1
λ μ μ§μ¬μ‘κ³ λ―Έλ€μ¨μ΄κ° λμ§ μ΄ν΄νκ³ μ΄λ»κ² μ°λμ§ μκ²λλ©΄ 곧 μ°λ¦¬κ° μμνλλλ‘ λμνκ²λλ€.
λ€μ: 09_middleware.md