当前位置: 首页 > news >正文

传统企业网站建设运营分析做响应式网站最大宽度

传统企业网站建设运营分析,做响应式网站最大宽度,深圳龙华租房,哪里做网站比较好文章目录 为什么需要子传父通信#xff1f;方法一#xff1a;回调函数#xff08;最常用#xff09;基础示例实际场景#xff1a;待办事项列表 方法二#xff1a;使用useRef传递引用方法三#xff1a;Context API#xff08;跨层级通信#xff09;方法四#xff1a;自… 文章目录 为什么需要子传父通信方法一回调函数最常用基础示例实际场景待办事项列表 方法二使用useRef传递引用方法三Context API跨层级通信方法四自定义Hook状态逻辑复用最佳实践与注意事项1. 回调函数命名规范2. 性能优化3. TypeScript类型定义4. 错误处理 选择合适的方法 在React开发中组件间的通信是一个核心概念。虽然React的数据流是单向的从父组件流向子组件但在实际开发中我们经常需要将子组件的数据或状态变化传递给父组件。本文将详细介绍React中实现子传父通信的几种方法。 为什么需要子传父通信 在实际开发中子传父通信的场景非常常见 表单输入框的值需要传递给父组件处理子组件的用户操作需要影响父组件的状态列表项的删除、编辑操作需要通知父组件更新数据模态框、弹窗的显示/隐藏状态控制 方法一回调函数最常用 这是最经典也是最常用的方法。父组件定义一个函数通过props传递给子组件子组件在需要时调用这个函数。 基础示例 // 父组件 function ParentComponent() {const [message, setMessage] useState();const handleChildMessage (childData) {setMessage(childData);console.log(收到子组件消息:, childData);};return (divh2父组件/h2p来自子组件的消息: {message}/pChildComponent onSendMessage{handleChildMessage} //div); }// 子组件 function ChildComponent({ onSendMessage }) {const [inputValue, setInputValue] useState();const handleSubmit () {if (inputValue.trim()) {onSendMessage(inputValue);setInputValue();}};return (divh3子组件/h3inputtypetextvalue{inputValue}onChange{(e) setInputValue(e.target.value)}placeholder输入消息/button onClick{handleSubmit}发送给父组件/button/div); }实际场景待办事项列表 // 父组件 - 待办事项管理 function TodoApp() {const [todos, setTodos] useState([{ id: 1, text: 学习React, completed: false },{ id: 2, text: 写技术博客, completed: false }]);const handleToggleTodo (id) {setTodos(todos.map(todo todo.id id ? { ...todo, completed: !todo.completed } : todo));};const handleDeleteTodo (id) {setTodos(todos.filter(todo todo.id ! id));};return (divh1待办事项/h1{todos.map(todo (TodoItemkey{todo.id}todo{todo}onToggle{handleToggleTodo}onDelete{handleDeleteTodo}/))}/div); }// 子组件 - 单个待办事项 function TodoItem({ todo, onToggle, onDelete }) {return (div style{{ padding: 10px, border: 1px solid #ccc, margin: 5px 0,textDecoration: todo.completed ? line-through : none}}span{todo.text}/spanbutton onClick{() onToggle(todo.id)}{todo.completed ? 取消完成 : 标记完成}/buttonbutton onClick{() onDelete(todo.id)}删除/button/div); }方法二使用useRef传递引用 当需要直接访问子组件的方法或属性时可以使用useRef和forwardRef。 import { useRef, forwardRef, useImperativeHandle } from react;// 子组件使用forwardRef const ChildComponent forwardRef((props, ref) {const [count, setCount] useState(0);useImperativeHandle(ref, () ({getCount: () count,resetCount: () setCount(0),incrementCount: () setCount(prev prev 1)}));return (divp计数: {count}/pbutton onClick{() setCount(prev prev 1)}增加/button/div); });// 父组件 function ParentComponent() {const childRef useRef();const handleGetChildData () {const childCount childRef.current.getCount();alert(子组件当前计数: ${childCount});};const handleResetChild () {childRef.current.resetCount();};return (divChildComponent ref{childRef} /button onClick{handleGetChildData}获取子组件数据/buttonbutton onClick{handleResetChild}重置子组件/button/div); }方法三Context API跨层级通信 当组件层级较深时使用Context API可以避免props逐层传递的问题。 import { createContext, useContext, useState } from react;// 创建Context const DataContext createContext();// 提供者组件 function DataProvider({ children }) {const [sharedData, setSharedData] useState();const updateData (newData) {setSharedData(newData);};return (DataContext.Provider value{{ sharedData, updateData }}{children}/DataContext.Provider); }// 父组件 function ParentComponent() {const { sharedData } useContext(DataContext);return (divh2父组件/h2p共享数据: {sharedData}/pMiddleComponent //div); }// 中间组件 function MiddleComponent() {return (divh3中间组件/h3DeepChildComponent //div); }// 深层子组件 function DeepChildComponent() {const { updateData } useContext(DataContext);const [inputValue, setInputValue] useState();const handleSubmit () {updateData(inputValue);setInputValue();};return (divh4深层子组件/h4inputtypetextvalue{inputValue}onChange{(e) setInputValue(e.target.value)}/button onClick{handleSubmit}更新共享数据/button/div); }// 应用根组件 function App() {return (DataProviderParentComponent //DataProvider); }方法四自定义Hook状态逻辑复用 将通信逻辑封装到自定义Hook中便于复用和管理。 // 自定义Hook function useParentChildCommunication(initialValue ) {const [value, setValue] useState(initialValue);const updateValue (newValue) {setValue(newValue);};return [value, updateValue]; }// 父组件 function ParentComponent() {const [childMessage, setChildMessage] useParentChildCommunication();return (divh2父组件/h2p子组件消息: {childMessage}/pChildComponent onMessage{setChildMessage} //div); }// 子组件 function ChildComponent({ onMessage }) {const [input, setInput] useState();const handleSend () {onMessage(input);setInput();};return (divinputtypetextvalue{input}onChange{(e) setInput(e.target.value)}/button onClick{handleSend}发送/button/div); }最佳实践与注意事项 1. 回调函数命名规范 使用on前缀onSubmit、onChange、onDelete语义化命名清楚表达函数的作用 2. 性能优化 使用useCallback优化回调函数避免不必要的重渲染 const handleChildMessage useCallback((message) {setMessage(message); }, []);3. TypeScript类型定义 interface ChildProps {onMessage: (message: string) void;onDelete?: (id: number) void; }const ChildComponent: React.FCChildProps ({ onMessage, onDelete }) {// 组件实现 };4. 错误处理 在回调函数中添加适当的错误处理 const handleChildData (data) {try {if (!data || typeof data ! string) {throw new Error(Invalid data format);}setParentData(data);} catch (error) {console.error(处理子组件数据时出错:, error);} };选择合适的方法 回调函数最常用适合简单的父子通信useRef需要直接访问子组件方法时使用Context API跨多层组件通信避免props drilling自定义Hook需要复用通信逻辑时使用
http://www.zqtcl.cn/news/30491/

相关文章:

  • 洛阳网站在哪备案建站模板哪个好
  • 网站开发合同管辖权异议沈阳网站建设哪家好
  • 在线教育网站怎样建设长沙市云网站建设
  • 成都装修网站制作价格网站维护入门教程
  • 网站建设公司排名前十iis7搭建网站
  • 长沙手机网站建设哪些免费制作贺卡的app
  • 做百度手机网站关键词排名徐州最新通知
  • 中国建设银行官方网站网上银行校园网站系统建设需求
  • 建网站 域名什么是互联网营销
  • 徐州网站建设xlec2021不良正能量免费网站app
  • 无人机网站建设软件工程师薪资
  • 企业手机网站案例佛山高端网站制作
  • 单页网站如何优化网站建设综合实训案例
  • 1元建站餐饮公司的网站建设
  • 2017网站icp备案安徽阜阳网站建设公司
  • 临沂网站制中国建筑英才网
  • 湖北省建设厅网站首页wordpress如何实现微信支付宝
  • 国外创意海报设计网站广东新闻联播主持人
  • 毕设做微课资源网站设计可以吗下述不属于网页制作工具
  • 移动端网站开发最好的环境网站推广优化怎么做最好
  • 网站设计的初衷网站上怎么做产品介绍
  • 增强网站互动it外包运维服务
  • 做网站上凡科网站公告设计
  • 宁波网站设计公司可以做课后作业的网站
  • 手机图片网站模板哪个网站可以找到毕业设计
  • 重庆航运建设发展有限公司 网站自学做网站的书
  • 怎么给网站加ico图标邯郸网站优化
  • 做生存曲线的网站wordpress移动端seo优化
  • h5可视化拖拽生成工具seo基础知识培训视频
  • 临汾万词霸屏网站建设免费咨询网站建设与规划周志总结