我在Redux中有一些问题,我想创建一个GET_TODO动作,以在每次调用时将数据从数据库中移入我的存储中。每次调用该函数时,都会清除基本状态,并且将我数据库中的所有数据都放入其中。
import { combineReducers } from "redux";
import { todolistFilters } from "./actions";
import axios from "axios";
const todos = (state = [], action) => {
switch (action.type) {
case "GET_TODO":
axios
.get("http://localhost:5000/urls/todos")
.then((res) => {
if (res.data) {
console.log(res.data);
}
})
.catch((err) => console.log(err));
return [];
case "ADD_TODO":
return [
...state,
{
id: action.id,
text: action.text,
completed: false,
},
];
case "TOGGLE_TODO":
return state.map((todo) =>
todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
);
case "DELETE_TODO":
return state.filter((todo) => todo.id !== action.id);
default:
return state;
}
};
const todolistFilter = (state = todolistFilters.SHOW_ALL, action) => {
switch (action.type) {
case "SET_TODOLIST_FILTER":
return action.filter;
default:
return state;
}
};
export default combineReducers({
todos,
todolistFilter,
});
来自JSON中我的Db的数据
[
{
"todo_id": "5f44b1fc00d1180f9c5bb18e",
"content": "first sending to with sasasddaame id",
"enable": false
},
{
"todo_id": "5f44b4e0cd556e1e7465431e",
"content": "first sending to with sasasddaame id",
"enable": false
},
{
"todo_id": "5f46513ad91b1b16a009016d",
"content": "first sending to with sasasddaame id",
"enable": false
}
]
因此,在执行“ GET_TODO”之后,我应该会看到它,并且可以通过“ ADD_TODO”操作在其上添加更多内容