云南建设厅网站职称评定,品牌网站建设有哪些内容,那个网站可以做网页,布恩网站删除文章目录render-props 模式props 方式children 方式(推荐)Hoc#xff08;高阶组件#xff09;使用步骤示例props 丢失解决方案自定义 hook1.只执行一次2.防抖hook高阶组件与自定义hook有什么区别相同点不同点React 中代码逻辑复用有三种方式#xff0c;render-props, Hoc高阶组件使用步骤示例props 丢失解决方案自定义 hook1.只执行一次2.防抖hook高阶组件与自定义hook有什么区别相同点不同点React 中代码逻辑复用有三种方式render-props, Hoc·自定义hooks·
注意: render-props, Hoc这两种方式不是新的API而是利用React自身特点的编码技巧演化而成的固定模式(写法
render-props 模式
注意: 并不是该模式叫 render props 就必须使用名为 render 的 prop实际上可以使用任意名称的 prop
props 方式
封装一个 render-props 模式下的鼠标移动得到鼠标当前移动位置
// 封装的组件
class Mounse extends React.Component {state {x: 0,y: 0}componentDidMount(){window.addEventListener(mousemove, this.mouseMove)}componentWillUnmount(){window.removeEventListener(mousemove, this.mouseMove)}mouseMove (e) {this.setState({x: e.clientX,y: e.clientY}) } render(){renten this.props.render(this.state)}
}// 使用
export default function Index() {return (h1Mouns render{(mouse){return px: { mouse.x }----y: { mouse.y }/p}} / )
}children 方式(推荐)
// 封装的组件
class Mounse extends React.Component {state {x: 0,y: 0}componentDidMount(){window.addEventListener(mousemove, this.mouseMove)}componentWillUnmount(){window.removeEventListener(mousemove, this.mouseMove)}mouseMove (e) {this.setState({x: e.clientX,y: e.clientY}) } render(){renten this.props.children(this.state)}
}使用
// 使用
export default function Index() {return (h1Mouns{(mouse){px: { mouse.x }----y: { mouse.y }/p}}/Mouns )
}Hoc高阶组件
高阶组件使用一个函数接收要包装的组件返回一个增强后的组件
使用步骤
创建一个函数以 with 开头指定函数参数函数参数为一个组件组件以大写字母开头在函数内创建一个类组件提供状态逻辑代码并返回
示例
function WithMounse(Com) {class Mounse extends PureComponent {state {x: 0,y: 0}componentDidMount(){window.addEventListener(mousemove,this.handleMonve)}componentWillUnmount(){window.removeEventListener(mousemove, this.handleMonve)}handleMonve e { this.setState({x: e.clientX,y: e.clientY})}render(){return Com {...this.state} /}}return Mounse /}const Foo props { return p{props.x}...{props.y}/p}const EndCom WithMounse(Foo).type// 调用class App extends PureComponent {render() {return (EndCom /)}}props 丢失
问题示范
由图片可以看出在高阶组件中传入一个 props 属性 a 1 在组件里面接收不到这就是属性丢失 解决方案
在高阶组件封装的时对props属性再次进行传递
示例 Com {...this.state} {...this.props} /function WithMounse(Com) {class Mounse extends PureComponent {state {x: 0,y: 0}componentDidMount(){window.addEventListener(mousemove,this.handleMonve)}componentWillUnmount(){window.removeEventListener(mousemove, this.handleMonve)}handleMonve e { this.setState({x: e.clientX,y: e.clientY})}render(){return Com {...this.state} {...this.props} /}}return Mounse /}
自定义 hook
hook 是react16.8 的新特性它可以在你不编写class组件的情况下使用state一级其他的React的特性
通过自定义hook,可以将组件逻辑提取到可重复的函数中
注意自定义hook 一定以 use 开头例如 useDebonce,useQuery等等
下面是几个自定义 hook 的封装
1.只执行一次
export const useMount (callback: () void) {useEffect(() {callback()}, [])
}使用 useMount((){// 数据请求})2.防抖hook
export const useDebonce T(value: T, delay?: number): T {const [debounce, setDebounce] useState(value)useEffect((){let timer setTimeout((){setDebounce(value)}, delay)return () clearTimeout(timer) },[value, delay])return debounce
}使用 const changeValue 改变所依赖的值const Debonce useDebonce(changeValue, 300)useEffect((){console.log(changeValue)},[Debonce])高阶组件与自定义hook有什么区别
相同点
都是对组件逻辑的封装达到组件逻辑复用的目的
不同点
定义方式不同高阶组件以with开头自定义hook以 use开头特性不同高阶组件是类组件中的总结出来的一种编码技巧自定义hook 是 react16.8 后出来的新特性使用场景不同 高阶组件一般在函数组件中使用自定义hook只有函数组件中有返回值不同高阶组件的返回值是一个组件自定义hook的返回值可有可无 文章转载自: http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn