开发者问题收集

undefined 不是对象(评估'_this.props.type')react native

2020-01-28
119

我是 React Native 的新手,我正在构建一个带有登录和注册界面的应用程序,尝试使用(this.props.type)来控制签名/注册按钮,但我的代码中出现错误,希望你们帮助我:)

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  TextInput,
  TouchableOpacity,
  Text
} from 'react-native';

const Form: () => React$Node = () => {

    return (
    <View style={styles.container}>
        <TextInput style={styles.inputBox}
                   underlineColorAndroid= 'rgba(0,0,0,0)'
                   placeholder= "Phone Number"
                   autoCorrect={true}
                   textContentType="telephoneNumber"
                   keyboardType="numbers-and-punctuation"
                   placeholderTextColor = "#ffffff"
        />

        <TextInput style={styles.inputBox}
                   underlineColorAndroid= 'rgba(0,0,0,0)'
                   placeholder= "Email"
                   autoCorrect={true}
                   textContentType="emailAddress"
                   keyboardType="email-address"
                   placeholderTextColor = "#ffffff"
        />

        <TextInput style={styles.inputBox}
                   underlineColorAndroid= 'rgba(0,0,0,0)'
                   placeholder= "Email Password"
                   secureTextEntry={true}
                   placeholderTextColor = "#ffffff"
        />

        <TouchableOpacity style={styles.button}>
                  <Text style={styles.buttonText}>{this.props.type} </Text>
        </TouchableOpacity>
    </View>
    );
};

我也在登录和注册文件中使用了这些行:

<Form types="SignUp"/>
<Form types="SignIn"/>
2个回答

更改 Form 以接收参数

const Form: ({ types }) => React$Node = () => {

然后像

<TouchableOpacity style={styles.button}>
  <Text style={styles.buttonText}>{types} </Text>
</TouchableOpacity>

那样使用它并呈现您的表单

<Form types="SignUp"/>
<Form types="SignIn"/>

没有 typescript 的表单组件

const Form = ({ types }) => (
  <View style={styles.container}>
    <TextInput
      style={styles.inputBox}
      underlineColorAndroid="rgba(0,0,0,0)"
      placeholder="Phone Number"
      autoCorrect={true}
      textContentType="telephoneNumber"
      keyboardType="numbers-and-punctuation"
      placeholderTextColor="#ffffff"
    />

    <TextInput
      style={styles.inputBox}
      underlineColorAndroid="rgba(0,0,0,0)"
      placeholder="Email"
      autoCorrect={true}
      textContentType="emailAddress"
      keyboardType="email-address"
      placeholderTextColor="#ffffff"
    />

    <TextInput
      style={styles.inputBox}
      underlineColorAndroid="rgba(0,0,0,0)"
      placeholder="Email Password"
      secureTextEntry={true}
      placeholderTextColor="#ffffff"
    />

    <TouchableOpacity style={styles.button}>
      <Text style={styles.buttonText}>{types} </Text>
    </TouchableOpacity>
  </View>
);
Junius L
2020-01-28

问题在于,您在文本中获取了 this.props.type <Text style={styles.buttonText}>{this.props.type} </Text> ,但您传递的类型为 <Form types="SignUp"/>

因此,将文本内部更改为

<Text style={styles.buttonText}>{this.props.types} </Text>

希望有帮助

Gaurav Roy
2020-01-28