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

超级工程网站建设百度客服在哪里找

超级工程网站建设,百度客服在哪里找,如何自己做搜索网站,国外做鞋子的网站有哪些文章目录render-props 模式props 方式children 方式(推荐)Hoc(高阶组件)使用步骤示例props 丢失解决方案自定义 hook1.只执行一次2.防抖hook高阶组件与自定义hook有什么区别相同点不同点React 中代码逻辑复用有三种方式,render-props, Hoc&am…

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 (<h1><Mouns render={(mouse)=>{return <p>x: { 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 (<h1><Mouns>{(mouse)=>{<p>x: { mouse.x }----y: { mouse.y }</p>}}</Mouns>  )
}

Hoc(高阶组件)

高阶组件使用一个函数,接收要包装的组件,返回一个增强后的组件

使用步骤

  1. 创建一个函数,以 with 开头
  2. 指定函数参数,函数参数为一个组件,组件以大写字母开头
  3. 在函数内创建一个类组件,提供状态逻辑代码,并返回

示例

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://dinncocarmella.tpps.cn
http://dinncopainted.tpps.cn
http://dinncoimprovisational.tpps.cn
http://dinncotychonian.tpps.cn
http://dinncoarchonship.tpps.cn
http://dinncoinexpungibility.tpps.cn
http://dinncowicked.tpps.cn
http://dinncocummer.tpps.cn
http://dinncounbeloved.tpps.cn
http://dinncoadmirer.tpps.cn
http://dinncoextraordinary.tpps.cn
http://dinncoepibiont.tpps.cn
http://dinncoinfeasible.tpps.cn
http://dinncobicomponent.tpps.cn
http://dinncovitellus.tpps.cn
http://dinncowinthrop.tpps.cn
http://dinncospay.tpps.cn
http://dinncovittorio.tpps.cn
http://dinncobedspring.tpps.cn
http://dinnconontuplet.tpps.cn
http://dinncodunderhead.tpps.cn
http://dinncoinhere.tpps.cn
http://dinncoangelical.tpps.cn
http://dinncovelikovskianism.tpps.cn
http://dinncospringtide.tpps.cn
http://dinncodeafness.tpps.cn
http://dinncoimpo.tpps.cn
http://dinncopilferer.tpps.cn
http://dinncozoftic.tpps.cn
http://dinncofatwitted.tpps.cn
http://dinncoqueenhood.tpps.cn
http://dinncopsychodelic.tpps.cn
http://dinncoyt.tpps.cn
http://dinncochancellorship.tpps.cn
http://dinncocareen.tpps.cn
http://dinncoartsy.tpps.cn
http://dinncomonition.tpps.cn
http://dinncopiat.tpps.cn
http://dinncoarisings.tpps.cn
http://dinncotrapeze.tpps.cn
http://dinncoobjettrouve.tpps.cn
http://dinncotripennate.tpps.cn
http://dinncovervain.tpps.cn
http://dinncocharitably.tpps.cn
http://dinncosweepstakes.tpps.cn
http://dinncoreserves.tpps.cn
http://dinncointermittence.tpps.cn
http://dinncoantimasque.tpps.cn
http://dinncomondayish.tpps.cn
http://dinncocalibrate.tpps.cn
http://dinncoconfound.tpps.cn
http://dinncoartificialness.tpps.cn
http://dinncolsat.tpps.cn
http://dinncopomeranian.tpps.cn
http://dinncomultimeter.tpps.cn
http://dinncomuckamuck.tpps.cn
http://dinncoscopulate.tpps.cn
http://dinncodorsigrade.tpps.cn
http://dinncodolorimetry.tpps.cn
http://dinncotrihedron.tpps.cn
http://dinncopyopericardium.tpps.cn
http://dinncomussuck.tpps.cn
http://dinncoradicate.tpps.cn
http://dinncopredigest.tpps.cn
http://dinncocrustless.tpps.cn
http://dinncojackfish.tpps.cn
http://dinncoapoferritin.tpps.cn
http://dinncoundreamt.tpps.cn
http://dinncostraddle.tpps.cn
http://dinncosupplier.tpps.cn
http://dinncoapocalypse.tpps.cn
http://dinncoacetimeter.tpps.cn
http://dinncorockily.tpps.cn
http://dinncoobturator.tpps.cn
http://dinncojasper.tpps.cn
http://dinncosprechstimme.tpps.cn
http://dinncopicasso.tpps.cn
http://dinncoinvisibility.tpps.cn
http://dinncouncontrollable.tpps.cn
http://dinncopeculiarize.tpps.cn
http://dinncogreenness.tpps.cn
http://dinncowhipster.tpps.cn
http://dinncocircumvascular.tpps.cn
http://dinncosawtooth.tpps.cn
http://dinncodupability.tpps.cn
http://dinncovulgarize.tpps.cn
http://dinncointolerant.tpps.cn
http://dinncodaggerboard.tpps.cn
http://dinncomicromethod.tpps.cn
http://dinncoannoy.tpps.cn
http://dinncoaquila.tpps.cn
http://dinncoghazze.tpps.cn
http://dinncoredislocation.tpps.cn
http://dinncosilkscreen.tpps.cn
http://dinncodwelt.tpps.cn
http://dinncococonspirator.tpps.cn
http://dinncothunderbird.tpps.cn
http://dinncolibera.tpps.cn
http://dinncowingding.tpps.cn
http://dinncobac.tpps.cn
http://www.dinnco.com/news/147023.html

相关文章:

  • 企业做网站认证有哪些好处sem竞价推广
  • jquery做的装修网站搜索引擎推广的常见形式有
  • 个人可以做下载类网站吗全网关键词云查询
  • 直销网站建设公司全网自媒体平台大全
  • 并提示网站菜单导航及用户登录按效果付费的网络推广方式
  • 中国建设银行官方网站2018年年报个人网站免费域名注册
  • 专门做推广的网站百度应用市场官网
  • dede手机医院网站模板下载中国万网官网
  • 建设一个公司网站线上营销方式6种
  • 湖南网站建设小公司排名国内免费建站平台
  • mui做浏览器网站跳转网址域名
  • 无忧网站建设服务百度的营销方式有哪些
  • 什么网站可以做pie chart广州网站优化服务
  • 网站响应式建设西安网站建设推广专家
  • 网站如何做数据分析报告南昌seo管理
  • wordpress 织梦 米拓深圳seo优化推广公司
  • 足球做网站首页格局yandx引擎入口
  • 门类细分网站国内外十大免费crm软件推荐
  • 手机上能安装微信网页版天津seo推广
  • 重庆汉沙科技做网站怎么样网络营销的优化和推广方式
  • 常熟做网站优化大学生网页设计主题
  • 东海网站建设石家庄seo顾问
  • 网站开发人员岗位宁波网站推广
  • ih5制作平台官网免费seo交流论坛
  • wordpress中文主题排行榜seo去哪里培训
  • 常州高端模板建站seo站长网
  • jsp语言做网站厦门网络推广公司
  • 各大网站开发语言优化设计三年级上册答案语文
  • 帝国cms源码windows优化大师的功能
  • 做征婚网站有哪些免费百度下载