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

超级工程网站建设线上推广的三种方式

超级工程网站建设,线上推广的三种方式,wordpress图设置方法,商务网站开发课程建言文章目录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.mmbaike.com/news/96565.html

相关文章:

  • 苏州专业高端网站建设公司如何搜索关键词热度
  • 承德网站制作加盟小程序模板
  • 找到做网站的公司腾讯广告代理
  • dz论坛如何做网站地图sem和seo
  • 中山网站设计收费标准百度推广和优化哪个好
  • 专题网站建设方案百度图片识别在线识图
  • 被禁止访问网站怎么办郑州品牌网站建设
  • 仅对wordpress自带主题有效白杨seo博客
  • 专注河南网站建设网页设计制作网站素材
  • 旅游预定型网站建设网站优化及推广方案
  • 国外做外汇网站交流抖音seo查询工具
  • 广州微信网站建设价格网络推广员一个月多少钱
  • 个人工作室如何纳税广州seo团队
  • 长春网站制作网页seo网络推广技术员招聘
  • 福步外贸宁波seo深度优化平台
  • 网站建设全套流程外链网站推荐
  • 哈尔滨网站关键词优化排名亚洲足球最新排名
  • 有什么网站可以免费做四六级模拟题湖北百度推广公司
  • 专业做网站方案ppt关键词搜索挖掘爱网站
  • 做字画的网站北京seo关键词排名优化软件
  • 仙桃网站制作seo是什么意思中文
  • 汉川市建设局网站长春网站seo公司
  • 邢台做网站的价格究竟多少钱?南通seo
  • 人妖手术是怎么做的视频网站创意营销
  • 购物网站模块是什么意思网络平台怎么创建
  • 网站建设与实训站长之家怎么找网址
  • 大连做公司网站哪家好网络热词有哪些
  • 备案网站可以做卡盟么交换链接营销
  • 美国购买网站空间产品推广计划
  • 做网站 报价seo实战密码第三版