我想知道为什么我的状态待办事项在 Redux 开发工具中名为 todo 而不是 todos .. 这个名字从何而来? 没有初始状态..我想知道..
我正在关注Stephen Grider udemy 类(class),但使用待办事项而不是流作为修订
为什么我必须通过state.todo
而不是state.todos
返回它??
Jsson服务器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;
}
};
actions/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('/');
};
最佳答案
https://github.com/HosMercury/todos/blob/master/src/reducers/index.js在这里,您将列表作为待办事项而不是待办事项传递。
在这里您可以检查沙盒中的控制台 https://codesandbox.io/s/github/HosMercury/todos
关于javascript - React Redux 状态对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431491/