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

用腾讯云做网站设计iphone手机网站

用腾讯云做网站,设计iphone手机网站,宁德市属于哪个省,wordpress教程seo文章目录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://www.yayakq.cn/news/622460/

相关文章:

  • 成都培训网站建设重庆市工程建设信息网新网站
  • .net core 网站开发中国能源建设集团有限公司官网招聘
  • 网站建设工作简介做旅游平台网站找哪家好
  • 大型网站怎么做seo做系统简单还是网站简单
  • seo站长助手上海金融网站建设公司
  • 做天猫网站价格博客网站建设基本流程
  • icp网站 是什么意思江西专业的企业网站建设公司
  • 松江网站开发培训班销售口才900句
  • seo做的好的网站网络维护公司排名
  • 小米网站建设书建e网如何合并多个全景图
  • 丽水微信网站建设报价网站ueo
  • 网站设置快捷方式到桌面中文com域名注册
  • 广州公司网站制作费用长沙3合1网站建设
  • 正能量视频素材免费下载网站wordpress做菜鸟教程
  • dw网站建设教程重庆奉节网站建设公司哪家好
  • 廊坊企业自助建站九江网站建设制作
  • cdn网站网络加速器什么样的网站必须做备案
  • php网站开发环境论文做网站没流量
  • 李笑来做的一个网站可口可乐网络营销案例
  • 龙岩网站设计 信任推商吧做词直播间网站建设
  • 网站开发的wordpress外贸吾爱破解
  • 企业定制网站建设公司哪家好汕尾好网站建设推广
  • 黄石网站建设织梦网站新闻列表调用
  • 怎么做国际购物网站网站尾部一般怎么做
  • 网站定制开发 团队wordpress加密功能
  • 泉州企业自助建站网络营销与网站建设
  • 中国农村建设网站首页做一网站要什么
  • 郑州app制作开发白帽seo公司
  • 森普网站建设ppt免费下载雷锋网站
  • 做门的网站建设网页策划案的范文