美文网首页
React高阶组件--render props、高阶组件(Rea

React高阶组件--render props、高阶组件(Rea

作者: 小王子__ | 来源:发表于2021-11-19 17:58 被阅读0次
React - render props和高阶组件

1,render props模式

使用步骤

  • 1,创建一个组件,在组件中提供复用的状态逻辑代码
  • 2,将要复用的状态作为props.render(state)方法的参数,暴露到组件外部
  • 3,使用props.render()的返回值作为要渲染的内容
class 组件名 extends React.Component {
  state = {}
  render() {
    return this.props.render(this.state)
  }
}

<组件名 render={i => {
  return (
    <div>{i.xx}</div>
  )
}}/>

示例:

class Mouse extends React.Component {
  state = {
    x: 0,
    y: 0
  }

  handleMouseMove = e => {
    this.setState({
      x: e.clientX,
      y: e.clientY
    })
  }
  // 监听鼠标移动事件
  componentDidMount() {
    window.addEventListener('mousemove', this.handleMouseMove)
  }
  render() {
    return this.props.render(this.state)
  }
}
class App extends React.Component {
  render() {
    return (
      <div>
        <h1>哈哈哈哈</h1>
        <Mouse render={i => {
          return (
            <div>鼠标位置:{i.x}, {i.y}</div>
          )
        }}/>

        <Mouse render={i => {
          return (
            <img src={img} style={{position: 'absolute', top: i.y - 100, left: i.x - 100}}></img>
          )
        }}/>
      </div>
    )
  }
}

children代替render属性

  • 并不是该模式叫render props就必须使用名为render的prop,实际上可以使用任意名称的prop
  • 把prop是一个函数并且告诉组件要渲染什么内容的技术叫做:render props模式
  • 推荐使用children代替render属性
class Mouse extends React.Component {
  state = {
    x: 0,
    y: 0
  }

  handleMouseMove = e => {
    this.setState({
      x: e.clientX,
      y: e.clientY
    })
  }
  // 监听鼠标移动事件
  componentDidMount() {
    window.addEventListener('mousemove', this.handleMouseMove)
  }

  // 代码优化项 2 
  // 在卸载的时候解除mousemove事件绑定
  componentWillUnmount() {
    window.removeEventListener('mousemove', this.handleMouseMove)
  }
  render() {
    // return this.props.render(this.state)
    // children代替render 
    return this.props.children(this.state)
  }
}

// 代码优化项 1
Mouse.prototypes = {
  children: PropTypes.func.isRequired,
}
class App extends React.Component {
  render() {
    return (
      <div>
        <h1>哈哈哈哈</h1>
        {/* <Mouse render={i => {
          return (
            <div>鼠标位置:{i.x}, {i.y}</div>
          )
        }}/> */}

        {/* children代替render */}
        <Mouse>
          {
            ({x, y}) => {
              return (
                <div>鼠标位置:{x}, {y}</div>
              )
            }
          }
        </Mouse>

        {/* <Mouse render={i => {
          return (
            <img src={img} style={{position: 'absolute', top: i.y - 100, left: i.x - 100}}></img>
          )
        }}/> */}

        {/* children代替render  */}
        <Mouse>
          {
            ({x, y}) => {
              return (
                <img src={img} style={{position: 'absolute', top: y - 100, left: x - 100}}></img>
              )
            }
          }
        </Mouse>
      </div>
    )
  }
}

2, 高阶组件(HOC)

目的是实现状态逻辑复用。

使用步骤:

  • 1,创建一个函数,名称约定以with开头
  • 2,指定函数参数,参数应该以大写字母开头(作为要渲染的组件)
  • 3,在函数内部创建一个类组件,提供复用的状态逻辑代码,并返回
  • 4,在该组件中,渲染参数组件,同时将状态通过prop传递给参数组件
  • 5,调用该高阶组件,传入要增强的组件,通过返回值拿到增强后的组件,并将其渲染到页面中
function withMouse(WrappendComponent) {
  class Mouse extends React.Component{
    return Mouse
  }
}

// Mouse组件的render方法中:
return <WrappendComponent {...this.state}/>

// 创建组件
const MousePosition = widthMouse(Position)

// 渲染组件
<MousePosition />

以上示例改为高阶组件:

// 创建高阶组件
function widthMouse(WrappedComponent) {
  // 该组件提供复用的状态逻辑
  class Mouse extends React.Component {
    state = {
      x: 0,
      y: 0
    }

    handleMouseMove = e => {
      this.setState({
        x: e.clientX,
        y: e.clientY
      })
    }
    componentDidMount() {
      window.addEventListener('mousemove', this.handleMouseMove)
    }

    componentWillUnmount() {
      window.removeEventListener('mousemove', this.handleMouseMove)
    }

    render() {
      return <WrappedComponent {...this.state}/>
    }
  }

  return Mouse
}

// 用来测试高阶组件 获取鼠标移动的位置的组件
const Position = props => (
  <p>
    鼠标当前位置: (x: {props.x}, y: {props.y})
  </p>
)

// 用来测试获取图片跟随鼠标动的组件
const ImgPosition = props => (
  <img src={img} style={{
    position: 'absolute',
    top: props.y - 100,
    left: props.x - 100
  }}></img>
)

// 获取增强后的组件
const MousePosition = widthMouse(Position)

const MouseImg = widthMouse(ImgPosition)

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>高阶组件</h1>
        <MousePosition/>
        <MouseImg/>
      </div>
    )
  }
}
设置displayName
  • 使用高阶组件存在的问题:得到的两个组件名称相同
  • 原因:默认情况下,React使用组件名称作为displayName
  • 解决方式: 为高阶组件设置displayName便于调试的时候区分不同的组件
  • displayName的作用:用于设置调试信息(React developer tools)
  • 设置方式
Mouse.displayName = `widthMouse${getDisplayName(WrappedComponent)}`

function getDisplayName(WrappedComponent) {
  return WrappedComponent.displayName || WrappedComponent.name || 'xiaowang'
}

以上示例改写为设置displayName

function widthMouse(WrappedComponent) {
  // 该组件提供复用的状态逻辑
  class Mouse extends React.Component {
    state = {
      x: 0,
      y: 0
    }

    handleMouseMove = e => {
      this.setState({
        x: e.clientX,
        y: e.clientY
      })
    }
    componentDidMount() {
      window.addEventListener('mousemove', this.handleMouseMove)
    }

    componentWillUnmount() {
      window.removeEventListener('mousemove', this.handleMouseMove)
    }

    render() {
      return <WrappedComponent {...this.state}/>
    }
  }
  // 设置displayName
  Mouse.displayName = `widthMouse${getDisplayName(WrappedComponent)}`  // widthMousePosition widthMouseImgPosition

  return Mouse
}

function getDisplayName(WrappedComponent) {
  console.log('WrappedComponent=====>', WrappedComponent.displayName)  // undefined undefined
  console.log('WrappedComponent=====>', WrappedComponent.name)  // Position ImgPosition
  return WrappedComponent.displayName || WrappedComponent.name || 'xiaoWangComponent'
}

// 用来测试高阶组件 获取鼠标移动的位置的组件
const Position = props => (
  <p>
    鼠标当前位置: (x: {props.x}, y: {props.y})
  </p>
)

// 用来测试获取图片跟随鼠标动的组件
const ImgPosition = props => (
  <img src={img} style={{
    position: 'absolute',
    top: props.y - 100,
    left: props.x - 100
  }}></img>
)

// 获取增强后的组件
const MousePosition = widthMouse(Position)

const MouseImg = widthMouse(ImgPosition)

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>高阶组件</h1>
        <MousePosition/>
        <MouseImg/>
      </div>
    )
  }
}
高阶组件 传递props
  • props丢失
  • 原因:高阶组件没有往下传递props
  • 解决方式:渲染WrappedComponent时,将state和this.props一起传递给组件
  • 传递方式
<WrappedComponent {...this.state} {...this.props}></WrappedComponent>

以上示例涉及到修改的代码:

function widthMouse(WrappedComponent) {
  // 该组件提供复用的状态逻辑
  // class Mouse extends React.Component {
  //   state = {
  //     x: 0,
  //     y: 0
  //   }

    // handleMouseMove = e => {
    //   this.setState({
    //     x: e.clientX,
    //     y: e.clientY
    //   })
    // }
    // componentDidMount() {
    //   window.addEventListener('mousemove', this.handleMouseMove)
    // }

    // componentWillUnmount() {
    //   window.removeEventListener('mousemove', this.handleMouseMove)
    // }

    render() {
      // Mouse组件中可以拿到other这个属性
      console.log('this.props=====>', this.props)
      return <WrappedComponent {...this.state} {...this.props}/>
    }
  }
  // 设置displayName
  // Mouse.displayName = `widthMouse${getDisplayName(WrappedComponent)}`  // widthMousePosition widthMouseImgPosition

  return Mouse
}

// function getDisplayName(WrappedComponent) {
//   console.log('WrappedComponent=====>', WrappedComponent.displayName)  // undefined undefined
//   console.log('WrappedComponent=====>', WrappedComponent.name)  // Position ImgPosition
//   return WrappedComponent.displayName || WrappedComponent.name || 'xiaoWangComponent'
// }

// 用来测试高阶组件 获取鼠标移动的位置的组件
const Position = props => {
  // position组件没有拿到other属性
  console.log('position组件:=====>', props)
  return (
    <p>
      鼠标当前位置: (x: {props.x}, y: {props.y})
    </p>
  )
}

// 用来测试获取图片跟随鼠标动的组件
// const ImgPosition = props => (
//   <img src={img} style={{
//     position: 'absolute',
//     top: props.y - 100,
//     left: props.x - 100
//   }}></img>
// )

// 获取增强后的组件
// const MousePosition = widthMouse(Position)

// const MouseImg = widthMouse(ImgPosition)

class App extends React.Component {
  render() {
    return (
      <div>
        {/* <h1>高阶组件</h1> */}
        <MousePosition other="哈哈哈"/>
        {/* <MouseImg/> */}
      </div>
    )
  }
}

相关文章

网友评论

      本文标题:React高阶组件--render props、高阶组件(Rea

      本文链接:https://www.haomeiwen.com/subject/spletrtx.html