【问题标题】:'undefined is not an object (evaluating 'props.deviceLocale')'未定义不是对象(评估'props.deviceLocale')
【发布时间】:2019-08-16 22:28:22
【问题描述】:

我正在尝试在单击提交按钮时验证文本字段。但我总是得到错误-

TypeError: undefined is not an object (evalating 'props.deviceLocale') on my emulator.

(我使用的是安卓模拟器)。

但是我在我的代码中没有使用“deviceLocale”。我不知道我的代码中是否需要它。

这是我的代码:

import React, {Component} from 'react';
import { View, Text, TouchableOpacity, TextInput } from 'react-native';
import ValidationComponent from 'react-native-form-validator';

export default class App extends ValidationComponent {
  
  constructor() {
    super()
    this.state = {
      name: "abc"
    }
  }

  _onPressButton() {
    this.validate({
      name: {minlength: 3, maxlength: 7, required: true},
    });
  }

  render() {
    return (

      <View>

        <TextInput 
          ref = "name"
          onChangeText = {(name) => this.setState({name})}
          value = {this.state.name}
        />

        <TouchableOpacity onPress = {this._onPressButton}>
          <Text>Submit</Text>
        </TouchableOpacity>

      </View>
    )
  }
}

错误捕捉:

【问题讨论】:

  • 请添加您的验证方法的代码,以及这个错误究竟是什么时候发生的?点击按钮?
  • @SurajMalviya,验证方法的代码是什么意思?我问是因为我是新手。我正在 _onPressButton() 中验证我唯一的文本字段。按下按钮时不会发生错误。它甚至在加载初始页面之前发生。

标签: android react-native android-emulator


【解决方案1】:

您甚至在加载组件之前就收到错误,请正确绑定您的_onPressButton,然后至少您的组件将正确安装,然后您即将出现的错误将随之而来,就像使用 this.validate 有点模棱两可对我来说,因为我在组件中看不到 validate 函数。

要绑定你的_onPressed,如下声明:

_onPressButton = () => {
  this.validate({
    name: {minlength: 3, maxlength: 7, required: true},
  });
}

当您的组件被挂载时,_onPressed 就会被调用。如果这有助于您至少在组件安装方面取得进展,请在 cmets 中告诉我。

已编辑: 此外,您的构造函数不向超级构造函数提供道具,

如下声明:

 constructor(props) {
   super(props);
   this.state = {
     name: "abc"
   }
 }

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 2019-09-26
    • 2019-12-12
    • 2017-01-16
    • 2020-08-06
    • 2020-01-09
    • 2017-05-03
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多