Flow混淆了React组件属性回调的类型

问题描述 投票:0回答:1

以某种方式使流混淆了Picker组件的onValueChange回调的类型。这是我的设置:

我以gender作为其中的一个字符串定义了State类型:

type State = {
  gender: string,
  weight: number
// code omitted

};

这是在构造函数中初始化的(不是强制性的吗?]

export default class CustomComponent extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = { gender: '', weight: 0 };
  }

  render() {

    const { gender, weight } = this.state;

// code omitted

并且在Picker组件中,我想像这样在onValueChange回调中设置性别状态:

 <Picker
   style={styles.inputElement}
   selectedValue={gender}
   onValueChange={itemvalue => {
   this.setState({ gender: itemvalue, isDirty: true });
    }}
     > // code omitted

但是Flow在itemvalue处显示错误:this.setState({性别:itemvalue,isDirty:true})] >>

Cannot call `this.setState` with object literal bound to `partialState` because  number [1] is incompatible with  string [2].Flow(InferError)

我也试图像这样键入定义项目值:

onValueChange={(itemvalue: string) => ....}

但这只是将错误移至新添加的string

似乎流认为itemvalue是一个数字,但它是一个字符串,对吗?

以某种方式使混淆选择器组件的onValueChangecallback的类型。这是我的设置:我定义了State类型,其中的性别为字符串:类型State = {性别:字符串,权重:...

javascript react-native flowtype
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.