我目前正在学习Redux,并且遇到了麻烦,我的问题是为什么你需要在没有它们的情况下应用程序工作的操作?他们添加了什么?在Redux中,使用actionCreators有什么好处?
import { createStore } from 'redux'
// ACTIONS
const addTodo = (text) => {
type: 'ADD_TODO',
text
}
const toggleTodo = (id) => {
type: 'TOGGLE_TODO',
id
}
// REDUCER
const todos = (state = [], action) => {
switch(action.type) {
case 'ADD_TODO':
return [
...state,
{
id: state.length,
text: action.text,
complete: false
}
]
default:
return state
}
}
// STORE
const store = createStore(todos)
// TEST
store.dispatch({ type: 'ADD_TODO', text: 'Test' })
console.log(store.getState())
您可以在反应组件和商店之间看到_“glue”_动作。 –
你是指动作创造者吗?正如你为什么需要addTodo当讯({类型:“Add_TODO”,文本:“测试”})的作品? –