首页 > 解决方案 > React Native 我在这里做错了什么

问题描述

我正在尝试实现一个功能组件。我在下面做,但我收到关于道具的错误。有人可以帮助我。

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

const TextField = () => {
    return (
        <Text> {this.props.title} </Text>
    )
}

export default TextField;

// APP Component
    import TextField from './Components/TextField'
    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
          <TextField title= "Simple App"/>
          </View>
        );
      }
    }

标签: reactjsreact-native

解决方案


原因是this.props未在功能组件中定义。他们接收道具作为参数。

改变你TextField的接受论点props和使用props.title

const TextField = (props) => {
    return (
        <Text> {props.title} </Text>
    )
}

推荐阅读