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

怎样搭建免费网站b2b平台有哪些网站

怎样搭建免费网站,b2b平台有哪些网站,一个做特卖的网站,wordpress微信登录插件下载失败自定义hook: 用来封装复用的逻辑,,自定义hook是以use开头的普通函数,,将组件中可复用的状态逻辑抽取到自定义的hook中,简化组件代码 常见自定义hook例子: 封装一个简单的计数器 import {useS…

自定义hook: 用来封装复用的逻辑,,自定义hook是以use开头的普通函数,,将组件中可复用的状态逻辑抽取到自定义的hook中,简化组件代码

常见自定义hook例子:

封装一个简单的计数器
import {useState} from "react";function useCounter(initialValue:number){const [count, setCount] = useState(0)const increment = ()=>setCount(prev=>prev+1)const decrement = ()=>setCount(prev=>prev-1)const reset = ()=>setCount(initialValue)return {count,increment,decrement,reset}
}export default useCounter
封装一个自动监听网页窗口变化的hooks
import {useEffect, useState} from "react";function useWindowSize(){const [size, setSize] = useState({width:window.innerWidth,height:window.innerHeight})useEffect(() => {const handleResize = ()=>{setSize({width: window.innerWidth,height: window.innerHeight})}window.addEventListener("resize",handleResize)return ()=> window.removeEventListener("resize",handleResize)}, []);return size
}export default useWindowSize
封装一个网络请求的hooks,返回请求的状态
import {useEffect, useState} from "react";
import axios from "axios";interface FetchResult<T>{data:T | null,loading:boolean,error: Error | null
}
function useFetch<T = any>(url:string):FetchResult<T>{const [data, setData] = useState<T | null>(null)const [loading, setLoading] = useState(true)const [error, setError] = useState<Error | null>(null)useEffect(() => {const fetchData = async ()=>{try {const response = await axios.get(url)console.log(response)setData(response.data)} catch (e) {setError(e as Error)} finally {setLoading(false)}}fetchData()}, [url]);return {data,loading,error}}export default useFetch
hook设置获取localStorage
import {useState} from "react";function useLocalStorage<T>(key:string,initialValue:T | null){const [storeValue, setStoreValue] = useState(()=>{try {var item = window.localStorage.getItem(key);return item ? JSON.parse(item) : initialValue} catch (e) {console.log(e)return initialValue}})const setValue = (val:T | null)=>{try {setStoreValue(val)if (val){window.localStorage.setItem(key, JSON.stringify(val))}else{window.localStorage.removeItem(key)}} catch (e) {console.log(e)}}return [storeValue,setValue] as const}export default useLocalStorage

调用hook:

import useCounter from "./useCounter";
import useWindowSize from "./useWindowSize";
import useFetch from "./useFetch";
import useLocalStorage from "./useLocalStorage";function CustomHook(){const {count,increment,decrement,reset} = useCounter(10)const {width,height} =useWindowSize()const {data,loading,error} = useFetch("http://localhost:9090/hello")const [token,setToken] = useLocalStorage<String | null>("token",null)if (loading) return <div>loading ....</div>if (error) return  <div>error: {error.message}</div>return (<div><div> {width}</div><div> {height}</div>{count}<button onClick={increment}>increment</button><button onClick={decrement}>decrement</button>{data}<h3> token is :{token}</h3><button onClick={()=>setToken("123")}>123</button></div>)
}export default CustomHook
http://www.dtcms.com/wzjs/4010.html

相关文章:

  • 手机怎么自己做网站百度如何发布信息推广
  • 网站商城建设要多少钱网站搭建需要多少钱
  • 网站外包维护一年多少钱多合一seo插件破解版
  • 做视频网站 许可苏州网站建设哪家靠谱
  • 做网站的风险分析内容营销是什么意思
  • 哈尔滨网站建设报价价格网络营销策划与推广
  • 常州市武进区城乡建设局网站seo小白入门
  • 建设网站的课题上海网络营销有限公司
  • 装饰设计网站推荐教育培训机构网站
  • 做网站的样版海曙seo关键词优化方案
  • 记事本做网站的代码百度seo优化工具
  • frontpg做网站好吗百度网站认证
  • 吴江区桃源镇做网站关键词指数批量查询
  • 外贸网站装修外贸营销网站
  • 沧州网站建设外贸网络推广图片
  • 网站演示网站代码自己怎么开网站
  • 北京顺义区住房和城乡建设委员会网站进一步优化落实
  • 做网站公司 包含了服务器费用吗杭州优化外包哪里好
  • 政府类网站制作怎么seo网站关键词优化
  • 律师做网络推广最好的网站有哪些免费b站软件推广网站
  • wordpress时间代码福州seo技术培训
  • wordpress做定制T恤的网站全达seo
  • 网站主页调用世界杯比分
  • 化妆品公司的网站建设的利益分析网上怎么推销自己的产品
  • 手机网站做多少钱营销技巧和营销方法视频
  • 用html建设网站近日发生的重大新闻
  • 免费云建站郑州做网站推广
  • 新疆建设安监局网站查询seo外贸网站制作
  • 让百度收录网站百seo排名优化
  • 专业外贸网站建设 诚信 青岛青岛seo软件