州内缺少财产,但只是流量的支柱

问题描述 投票:0回答:1
import * as React from 'react';

type Props = {
  number: number,
}

type State = {

}

class Other extends React.Component <State, Props> {
  render() {
    const { number } = this.props;
    return (
      <div className="container">
        {number}
      </div>
    );
  }
}

//

import * as React from 'react';
type Props = {

}

type State = {
  number: number,
  cards: Array<{suit: string} | {rank: number}>,
}

class Example extends React.Component <Props, State>{
     <Other
       number={this.state.number}
     />
}

所以,在与React集成后,我得到了这个奇怪的错误。我的Example组件通过number状态作为Other的支柱。在Other我告诉它,number是一个应该是类型编号的道具。然而,它抱怨并说它应该处于状态?

我得到它应该是原始组件中的状态,但为什么它希望它在新组件中的状态?肯定地说它处于状态是没有意义的,因为它只是我想呈现的只读道具

javascript reactjs typescript flowtype
1个回答
0
投票

因为你混淆了Other组件上的类型声明。第一个参数应该是props接口,第二个应该是state。在您的原始示例中,您传递的是State作为道具的声明,相反,Props作为州的定义。这就是为什么你会得到Other期望值处于状态而不是道具的错误。

目前:class Other extends React.Component <State, Props>

应该是:class Other extends React.Component <Props, State>

© www.soinside.com 2019 - 2024. All rights reserved.