首页 > 解决方案 > TypeError: undefined is not an object ('_this.props')

问题描述

我无法解决这个问题。你能帮助我吗。评估“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;
 }

当我触摸到按钮 onPrefixPress 时的红框

标签: javascripttypescriptreact-native

解决方案


看起来您正在尝试调用未定义的函数(通过道具传递)。使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;
  }

或检查为什么它未定义。


推荐阅读