我想知道為什么我的狀態(tài) todos在 redux 開發(fā)工具中命名為todo而不是todos .. 這個名字是從哪里來的?沒有初始狀態(tài)..我想知道..我正在關(guān)注Stephen Grider udemy 課程,但使用待辦事項而不是流作為修訂為什么我必須回到它state.todo不state.todos?Jsson 服務(wù)器 db.json 文件(api 文件) { "todos": [ { "title": "lorem ipsum ", "description": "lorem ipsum", "id": 4 } ] }todoReducer.js import _ from 'lodash'; import { CREATE_TODO, EDIT_TODO, FETCH_TODO, FETCH_TODOS, DELETE_TODO } from '../actions/types'; export default (state = {}, action) => { switch (action.type) { case FETCH_TODOS: return { ...state, ..._.mapKeys(action.payload, 'id') }; case CREATE_TODO: case FETCH_TODO: case EDIT_TODO: return { ...state, [action.payload.id]: action.payload }; case DELETE_TODO: return _.omit(state, action.payload); default: return state; } };動作/ index.js import todos from '../apis/todos'; import history from '../history'; import { SIGN_IN, SIGN_OUT, CREATE_TODO, EDIT_TODO, FETCH_TODO, FETCH_TODOS, DELETE_TODO } from './types'; export const signIn = userId => { return { type: SIGN_IN, payload: userId }; }; export const signOut = () => { return { type: SIGN_OUT }; }; export const fetchTodos = () => async dispatch => { const response = await todos.get('/todos'); dispatch({ type: FETCH_TODOS, payload: response.data }); }; export const createTodo = formValues => async dispatch => { const response = await todos.post('/todos', formValues); dispatch({ type: CREATE_TODO, payload: response.data }); history.push('/'); };
React Redux 狀態(tài)對象屬性
九州編程
2021-06-16 18:19:30