react中的高阶组件

1.高阶组件的作用:实现组件的状态逻辑复用
2.高阶组件的使用步骤:
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>

</head>

<body>
  <div id="root"></div>
  <script type="text/babel">
    // 高阶组件的基本使用
    // 1. 创建一个函数,以with开头
    // 2. 指定函数的参数,以大写字母开头
    // 3. 在函数内部创建一个类组件,同时将状态通过props传递给参数组件
    function withMouse(WrappedComponent) {
      class Mouse extends React.Component {
        state = {
          x: '',
          y: ''
        }
        handleMove = (e) => {
          this.setState({
            x: e.pageX,
            y: e.pageY
          })
        }
        componentDidMount() {
          window.addEventListener('mousemove', this.handleMove)
        }
        componentWillUnmount() {
          window.removeEventListener('mousemove', this.handleMove)
        }
        render() {
          return <WrappedComponent {...this.state} />
        }
      }
      return Mouse
    }

    // 创建一个包含渲染ui的组件
    function Position(props) {
      return (
        <div>
          <p>x: {props.x}</p>
          <p>y: {props.y}</p>
        </div>
      )
    }

    // 调用封装状态和逻辑的方法包装Position
    const MousePosition = withMouse(Position)

    class App extends React.Component {
      constructor(props) {
        super(props)
      }
      render() {
        return (
          <div>
            <h3>高阶组件</h3>
            <MousePosition></MousePosition>
          </div>
        )
      }
    }
    ReactDOM.render(<App />, document.getElementById('root'))
  </script>
</body>

</html>

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>

<body>
  <div id="root"></div>
  <script type="text/babel">
    // 高阶组件,实现组件的复用
    // 创建高阶组件的步骤
    // 1. 创建一个以with开头的函数, 参数值必须以大写字母开头
    function withMouse(WrapedComponent) {
      // 2. 封装一个带有状态和操作方法的组件
      class Mouse extends React.Component {
        state = {
          x: 0,
          y: 0
        }
        handleMove = (e) => {
          this.setState({
            x: e.pageX,
            y: e.pageY
          })
        }
        componentDidMount() {
          window.addEventListener('mousemove', this.handleMove)
        }

        render() {
          return <WrapedComponent {...this.state} />
        }
      }
      // 如果组件被复用多次 ,会存在一个问题, 多个组件名称一致,不方便进行调试,因此需要设置displayName,以区分组件的不同
      Mouse.displayName = `withMouse${getDisplayName(WrapedComponent)}`
      function getDisplayName(WrappedComponent) {
        return WrapedComponent.displayName || WrappedComponent.name || 'Component'
      }
      return Mouse
    }

    // 定义一个ui组件
    function Position(props) {
      return (
        <div>x: {props.x}, y: {props.y}</div>
      )
    }

    function Kill(props) {
      return (
        <div>
          <h2>x: {props.x}, y: {props.y}</h2>
        </div>
      )
    }
    const MousePosition = withMouse(Position)
    const TwoPosition = withMouse(Kill)

    class App extends React.Component {
      render() {
        return (
          <div>
            <MousePosition />
            <TwoPosition />
          </div>
        )
      }
    }
    ReactDOM.render(<App />, document.getElementById('root'))
  </script>
</body>

</html>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果需要传值,需要在封装的高阶组件返回值中添加{…this.props},否则,在是接收不到传递过来的值的,可以在ui组件中打印查看结果
在这里插入图片描述
完整代码如下

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>

<body>
  <div id="root"></div>
  <script type="text/babel">
    // 高阶组件,实现组件的复用
    // 创建高阶组件的步骤
    // 1. 创建一个以with开头的函数, 参数值必须以大写字母开头
    function withMouse(WrapedComponent) {
      // 2. 封装一个带有状态和操作方法的组件
      class Mouse extends React.Component {
        state = {
          x: 0,
          y: 0
        }
        handleMove = (e) => {
          this.setState({
            x: e.pageX,
            y: e.pageY
          })
        }
        componentDidMount() {
          window.addEventListener('mousemove', this.handleMove)
        }

        render() {
          return <WrapedComponent {...this.state} {...this.props}/>
        }
      }
      // 如果组件被复用多次 ,会存在一个问题, 多个组件名称一致,不方便进行调试,因此需要设置displayName,以区分组件的不同
      Mouse.displayName = `withMouse${getDisplayName(WrapedComponent)}`
      function getDisplayName(WrappedComponent) {
        return WrapedComponent.displayName || WrappedComponent.name || 'Component'
      }
      return Mouse
    }

    // 定义一个ui组件
    function Position(props) {
      console.log(props)
      return (
        <div>x: {props.x}, y: {props.y}</div>
      )
    }

    function Kill(props) {
      return (
        <div>
          <h2>x: {props.x}, y: {props.y}</h2>
        </div>
      )
    }
    const MousePosition = withMouse(Position)
    const TwoPosition = withMouse(Kill)

    class App extends React.Component {
      render() {
        return (
          <div>
            <MousePosition  value={19}/>
            <TwoPosition />
          </div>
        )
      }
    }
    ReactDOM.render(<App />, document.getElementById('root'))
  </script>
</body>

</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值