代码之家  ›  专栏  ›  技术社区  ›  Ajay Gaur

@withViewPort装饰器如何在React中工作?

  •  0
  • Ajay Gaur  · 技术社区  · 10 年前

    我正在阅读此装饰程序,发现@withViewPort用法为:

    import React from 'react';
    import withViewport from 'react-decorators/withViewport';
    
    @withViewport
    class MyComponent {
      render() {
        let { width, height } = this.props.viewport;
        return <div>Viewport: {width + 'x' + height}</div>;
      }
    }
    
    React.render(<MyComponent />, document.body);
    

    @withViewPort装饰器是如何工作的?它有状态吗?在调整窗口大小时,这些状态会改变吗?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Lee    10 年前

    它是一个高阶组件,添加了一个监听窗口的处理程序。根据组件调整大小。高级职员: https://github.com/kriasoft/react-decorators/blob/master/src/withViewport.js

    装饰工的操作与以下操作相同:

    const MyComposedComponent = withComponent(MyComponent);
    render(<MyComposedComponent />, document.body);