代码之家  ›  专栏  ›  技术社区  ›  Andrew

在从未使用过的构造函数中声明react props的正确方法

  •  1
  • Andrew  · 技术社区  · 6 年前

    通常,当我不使用 props 在一个 construtor ,我可以安全地将其保留为空白

    constructor() {
        super()
    }
    

    但typescript需要您定义它。如果我不使用它,我一直在做这样的事情:

    constructor(_: never) {
        super(_)
    }
    

    是否有一种正确和/或更正式的方式来声明你从未使用过的论点?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Estus Flask    6 年前

    can be underscored in TypeScript if they aren't used _ super

    never something else rather than a property that is never used

      constructor(...args) {
        super(...args);
        // ...
      }
    

    constructor

    props componentWillMount

    class SomeComponent extends React.Component {
      state = {
        foo: synchronousInitialization();
      };
    
      // isn't needed
      /*
      constructor(props) {
        super(props);
      }
      */
    
      async componentDidMount() {
        const bar = await asynchronousInitialization();
        this.setState({ ...this.state, bar });
      }
      ...
    }
    
        2
  •  1
  •   Matt McCutchen    6 年前

    props never