TypeError:undefined不是对象('_this.props')

时间:2019-07-31 11:27:27

标签: javascript typescript react-native

我不能解决这个问题。你能帮助我吗。 评估“ this.props.speciesSelection.modalize”

<BarcodeInput
        speciesSelection={this.props.speciesSelection}
        species={species[0]}
        barcode={{ manufacturerValue: "", codeValue: "" }}
        onChangeText={this.onChangeText}
      />


 class BarcodeInput extends React.Component<Props, State> {
  onPrefixPress = () => {
    Keyboard.dismiss();
    this.props.speciesSelection.modalize.open();
    this.props.speciesSelection.modalizeOpened = true;
 }

当我触摸PrefixPress上的按钮时出现红色框

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试调用未定义的函数(通过props传递)。 将speciesSelection属性设置为可选。

interface Props {
  species: Species;
  barcode: BarcodeState;
  speciesSelection?: any;
  onChangeText: (prop: keyof BarcodeState, value: string) => void;
}

interface State { }

class BarcodeInput extends React.Component<Props, State> {
  onPrefixPress = () => {
    Keyboard.dismiss();
    this.props.speciesSelection && this.props.speciesSelection.modalize.open();
    this.props.speciesSelection && this.props.speciesSelection.modalizeOpened = true;
  }

或检查为什么未定义。