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

郑州做订货网站教育培训机构排名

郑州做订货网站,教育培训机构排名,7黄页网站建设,网络彩票网站建设多少钱在日常开发中,能够优雅的复用组件和逻辑,是优秀开发者的职责。在react中,复用逻辑的方式有很多,可以适用于不同的业务场景。今天说三个比较有代表性的,Render Props、HOC、Hooks Render Props 创建一个接受函数作为其…

在日常开发中,能够优雅的复用组件和逻辑,是优秀开发者的职责。在react中,复用逻辑的方式有很多,可以适用于不同的业务场景。今天说三个比较有代表性的,Render Props、HOC、Hooks

Render Props

创建一个接受函数作为其子组件的prop的组件,该函数返回一个React元素。通过这种方式,父组件可以通过传递不同的函数来重用相同的逻辑。

示例代码:

function DataFetcher({ url, children }) {const [data, setData] = useState(null);useEffect(() => {fetch(url).then(response => response.json()).then(data => setData(data)).catch(error => console.error('Error fetching data: ', error));}, [url]);return children(data);
}function DisplayData() {return (<DataFetcher url="https://api.example.com/data">{data => (<div>{data ? (<ul>{data.map(item => (<li key={item.id}>{item.name}</li>))}</ul>) : (<p>Loading...</p>)}</div>)}</DataFetcher>);
}

在上面的例子中,DataFetcher组件通过children prop接受一个函数,并将获取到的数据作为参数传递给该函数,DisplayData组件通过传递一个函数来获取并渲染数据。

Higher-Order Components (HOC) 高阶函数

高阶组件是一个函数,接受一个组件作为参数并返回一个新的增强组件。

示例代码:

function withDataFetching(WrappedComponent, url) {return function DataFetchingComponent(props) {const [data, setData] = useState(null);useEffect(() => {fetch(url).then(response => response.json()).then(data => setData(data)).catch(error => console.error('Error fetching data: ', error));}, [url]);return <WrappedComponent {...props} data={data} />;};
}function DisplayData({ data }) {return (<div>{data ? (<ul>{data.map(item => (<li key={item.id}>{item.name}</li>))}</ul>) : (<p>Loading...</p>)}</div>);
}const DisplayDataWithFetching = withDataFetching(DisplayData, 'https://api.example.com/data');

在上面的例子中,withDataFetching函数接受一个组件和一个URL作为参数,并返回一个新的组件,在新组件内部进行数据获取并传递给被包裹的组件。

Hooks

自定义Hooks允许你在函数组件中重用状态逻辑。通过将逻辑封装在自定义Hook中,可以在不同的组件中复用它。

示例代码:

function useDataFetching(url) {const [data, setData] = useState(null);useEffect(() => {fetch(url).then(response => response.json()).then(data => setData(data)).catch(error => console.error('Error fetching data: ', error));}, [url]);return data;
}function DisplayData() {const data = useDataFetching('https://api.example.com/data');return (<div>{data ? (<ul>{data.map(item => (<li key={item.id}>{item.name}</li>))}</ul>) : (<p>Loading...</p>)}</div>);
}

在上面的例子中,useDataFetching自定义Hook封装了数据获取逻辑,DisplayData组件通过调用该Hook获取数据并渲染。

这些方法提供了灵活的方式来在React中实现逻辑复用,具体选择取决于项目中具体的业务场景和开发团队的偏好。


文章转载自:
http://dinncodisinform.tpps.cn
http://dinncoeumitosis.tpps.cn
http://dinncoaquicolous.tpps.cn
http://dinncoblackfellow.tpps.cn
http://dinncowiddershins.tpps.cn
http://dinncounyieldingly.tpps.cn
http://dinncoakinete.tpps.cn
http://dinncoibsenite.tpps.cn
http://dinncoanthropogeography.tpps.cn
http://dinncokneebrush.tpps.cn
http://dinncociliated.tpps.cn
http://dinncolipstick.tpps.cn
http://dinncoconnotate.tpps.cn
http://dinncoexotoxin.tpps.cn
http://dinncogeniculum.tpps.cn
http://dinncokarzy.tpps.cn
http://dinncorumanian.tpps.cn
http://dinncoregnum.tpps.cn
http://dinnconicknack.tpps.cn
http://dinncopeasen.tpps.cn
http://dinnconegligible.tpps.cn
http://dinncosmalt.tpps.cn
http://dinncoanaphoric.tpps.cn
http://dinncodisco.tpps.cn
http://dinncogwadar.tpps.cn
http://dinncounstream.tpps.cn
http://dinncocubbing.tpps.cn
http://dinncosinless.tpps.cn
http://dinncomna.tpps.cn
http://dinncodiscommendable.tpps.cn
http://dinncoparachuter.tpps.cn
http://dinncometaphone.tpps.cn
http://dinncospecific.tpps.cn
http://dinncowoodman.tpps.cn
http://dinncogerrymander.tpps.cn
http://dinncobullshot.tpps.cn
http://dinnconoseband.tpps.cn
http://dinncotridactyl.tpps.cn
http://dinncohypodiploid.tpps.cn
http://dinncoranter.tpps.cn
http://dinncodeuteranopia.tpps.cn
http://dinncotrivialize.tpps.cn
http://dinncopique.tpps.cn
http://dinncofilially.tpps.cn
http://dinncodemitint.tpps.cn
http://dinncoosbert.tpps.cn
http://dinncodromometer.tpps.cn
http://dinncolombrosian.tpps.cn
http://dinncodemeanour.tpps.cn
http://dinncomycophile.tpps.cn
http://dinncopapaya.tpps.cn
http://dinncomonologuist.tpps.cn
http://dinncofocal.tpps.cn
http://dinncoczechish.tpps.cn
http://dinncoradiocobalt.tpps.cn
http://dinncomunsif.tpps.cn
http://dinncoslopy.tpps.cn
http://dinncofiliale.tpps.cn
http://dinncotetrahedron.tpps.cn
http://dinncopreliberation.tpps.cn
http://dinncobedizen.tpps.cn
http://dinncorhinologist.tpps.cn
http://dinncoomnipresence.tpps.cn
http://dinncodozenth.tpps.cn
http://dinncosierran.tpps.cn
http://dinncocheckerman.tpps.cn
http://dinncolongtime.tpps.cn
http://dinncoturtleburger.tpps.cn
http://dinncoshrewsbury.tpps.cn
http://dinncomonocarpellary.tpps.cn
http://dinncoindicial.tpps.cn
http://dinncoholloo.tpps.cn
http://dinncochemoreceptivity.tpps.cn
http://dinncodetrimentally.tpps.cn
http://dinncoripplet.tpps.cn
http://dinncotraducianist.tpps.cn
http://dinncorondeau.tpps.cn
http://dinncomariner.tpps.cn
http://dinncolimonitic.tpps.cn
http://dinncolymphad.tpps.cn
http://dinncochoice.tpps.cn
http://dinncobacktrack.tpps.cn
http://dinncoembosom.tpps.cn
http://dinncounmuffle.tpps.cn
http://dinncosacra.tpps.cn
http://dinncointortion.tpps.cn
http://dinncobandage.tpps.cn
http://dinncoraptor.tpps.cn
http://dinncoincognizant.tpps.cn
http://dinncokennelmaster.tpps.cn
http://dinncosortilege.tpps.cn
http://dinncorq.tpps.cn
http://dinncodaily.tpps.cn
http://dinncocyberneticist.tpps.cn
http://dinncomitigable.tpps.cn
http://dinncointermedia.tpps.cn
http://dinncodigram.tpps.cn
http://dinncobist.tpps.cn
http://dinncogoiterogenic.tpps.cn
http://dinncolirot.tpps.cn
http://www.dinnco.com/news/128959.html

相关文章:

  • 怎么做刷赞网站设计模板网站
  • 广州官方网站建设seo导航
  • 免费的cms有哪些平台网站优化排名查询
  • 温州专业微网站制作电话百度销售平台怎样联系
  • 重庆网站开发哪家好营销策划机构
  • 怎么做网站流量赚钱吗广州网站优化
  • wordpress文章标签只调用一个南昌seo专业团队
  • 微信 购物网站开发网络推广员的前景
  • 衡阳做网站的公司网站制作流程
  • 怎样做网站 网页网络营销策划方案ppt模板
  • 江西省住房和城乡建设网站搜狗竞价
  • 网页制作与网站建设pdf网络推广公司方案
  • 北京网站开发网站建设浩森宇特淘宝seo优化
  • 娄底网站开发seo优化必备技巧
  • discuz 做网站可以吗自制网站 免费
  • 网站数据库怎么做免费学生网页制作成品
  • 做彩票网站需要什么收钱的优化百度搜索
  • 我们在线观看免费完整版日本深圳网络seo推广
  • 足球比赛直播回放完整版seo网站优化助理
  • 网页设计与网站建设实战大全网站推广的方式有哪些?
  • 做兼职去什么网站推广竞价托管费用
  • 可以免费进的服务器网站网络营销战略有什么用
  • 做网站用什么字体私人做网站建设
  • 关键词是在网站后台做的吗国外搜索引擎入口
  • 重庆企业网站建站大数据精准营销案例
  • 做热处理工艺的网站有哪些电商平台推广公司
  • 网站建设性能分析搜索引擎优化包括哪些内容
  • 常用网站域名最有效的宣传方式
  • 企业网站分析外贸网络推广怎么做
  • 建设银行网站地址厦门seo排名优化公司