物业管理系统和物业管理软件,淘宝关键词优化技巧教程,新建网站怎样绑定域名,现在的网站用什么程序做useReducer和useContext前面有单独介绍过#xff0c;上手不难#xff0c;现在我们把这两个api结合起来使用#xff0c;该怎么用#xff1f;还是结合之前的简易增删改查的demo#xff0c;熟悉vue的应该可以看出#xff0c;useReducer类似于vuex#xff0c;useContext类似…useReducer和useContext前面有单独介绍过上手不难现在我们把这两个api结合起来使用该怎么用还是结合之前的简易增删改查的demo熟悉vue的应该可以看出useReducer类似于vuexuseContext类似于vue中的inject和provided,来分析下思路。
实现效果
代码实现
文件拆解 组件入口文件 - index.js
import { TasksContext, TasksDispatchContext } from ./context;
import { useReducer } from react;
import { initialTasks } from ./taskLists;
import { taskReucers } from ./tasksReducer;
import AddTask from ./AddTask;
import TaskList from ./TaskList;
function State() {const [tasks, dispatch] useReducer(taskReucers, initialTasks);return (TasksContext.Provider value{tasks}TasksDispatchContext.Provider value{dispatch}AddTask /TaskList //TasksDispatchContext.Provider/TasksContext.Provider);
}export default State;AddTask.js
import { useState, useContext } from react;
import { TasksDispatchContext } from ./context;
let nextId 3;function AddTask() {let [msg, setMsg] useState();const dispatch useContext(TasksDispatchContext);const handleSubmit () {if (!msg) return;setMsg();dispatch({type: added,task: {id: nextId,text: msg,done: false,},});};const handleChange (e) {setMsg(e.target.value);};return (input typetext value{msg} onChange{handleChange} /button onClick{handleSubmit}添加/button/);
}export default AddTask;
context.js文件
import { createContext } from react;export const TasksContext createContext(null);
export const TasksDispatchContext createContext(null);taskReucers.js
export function taskReucers(state [], action) {switch (action.type) {case added:return [...state, action.task];case changed:return state.map((task) {if (task.id action.task.id) {return action.task;} else return task;});case deleted:return state.filter((task) task.id ! action.task.id);default:throw new Error(Action type not found);}
}taskListsData.js
export const initialTasks [{ id: 0, text: Philosopher’s Path, done: true },{ id: 1, text: Visit the temple, done: false },{ id: 2, text: Drink matcha, done: false },
];TaskList.js
import { useState, useContext } from react;
import Task from ./Task;
import { TasksContext } from ./context;function TaskList() {const [isEdit, setIsEdit] useState(false);const tasks useContext(TasksContext);const handleChangeValue (value) {};return (ul{tasks.map((task) {return Task key{task.id} task{task} /;})}/ul);
}export default TaskList;Task.js
import { useState, useContext } from react;
import { TasksDispatchContext } from ./context;
function Task({ task }) {const [isEdit, setIsEdit] useState(false);const dispatch useContext(TasksDispatchContext);let todoContent ;function handleChangeText(e) {dispatch({ type: changed, task: { id: task.id, text: e.target.value } });}function handleDeleteTask(id) {dispatch({ type: deleted, task: { id } });}if (isEdit) {todoContent (spaninput typetext value{task.text} onChange{handleChangeText} /button onClick{() setIsEdit(false)}完成/button/span);} else {todoContent (spanspan{task.text}/spanbutton onClick{() setIsEdit(true)}编辑/button/span);}return (li{todoContent}button onClick{() handleDeleteTask(task.id)}删除/button/li);
}export default Task;这样拆解后明显的业务更加清晰容易维护了给后续接手的人一目了然的理解思路。 没有了组件层级之间的繁琐的层层传递数据和方法代码结构也很清晰了。 进一步的优化业务代码
context.js的封装
import { createContext, useReducer } from react;
import { initialTasks } from ./taskListsData;
import { taskReucers } from ./tasksReducer;export const TasksContext createContext(null);
export const TasksDispatchContext createContext(null);export default function TasksProvider({ children }) {const [tasks, dispatch] useReducer(taskReucers, initialTasks);return (TasksContext.Provider value{tasks}TasksDispatchContext.Provider value{dispatch}{children}/TasksDispatchContext.Provider/TasksContext.Provider);
}入口文件index.js的优化
import AddTask from ./AddTask;
import TaskList from ./TaskList;
import TasksProvider from ./context;
function State() {return (TasksProviderAddTask /TaskList //TasksProvider);
}export default State;这样实现了同样的效果代码更加精简。 文章转载自: http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn