1 回答

TA贡献1775条经验 获得超11个赞
您需要在组件安装阶段发出 http 请求:
const todoList = ({ todos, dispatch }) => {
React.useEffect(() => {
let res = await axios.get("http://localhost:5000/urls/todos");
let data = await res.data;
dispatch({type: 'INIT_DATA', payload: data});
}, []);
return (
<div>
<ListGroup>
{todos.map((todo) => {
return (
<div key={todo.todo_id} className="">
<ListGroupItem
className="mt-1 mx-5 text-center rounded-pill inline"
color="success"
>
<h5 className=""> {todo.content}</h5>
<button type="button" className=" btn btn-dark rounded-pill ">
Dark
</button>
</ListGroupItem>
</div>
);
})}
</ListGroup>
</div>
);
};
你需要改变你的减速器:
const rootReducer = (state = [], action) => {
switch(action.type) {
case 'INIT_DATA': return action.payload;
default: return state;
}
};
这是做你想做的事情的一种简短方法,但建议用作action.typeconst 变量而不是硬编码字符串。
您还应该制作一个操作文件,您可以将您正在对应用程序中的待办事项列表执行的操作(如添加项目、删除等)发送到商店。
添加回答
举报