【问题标题】:TypeError: Unable to set property 'props' of undefined or null reference - react native componentTypeError:无法设置未定义或空引用的属性“道具” - 反应原生组件
【发布时间】:2020-02-10 07:17:33
【问题描述】:

我是 React Native 的新手。当我检查我的代码时,我正在从组件中学习但仍然得到错误的道具,即使在文档中使用道具组件反应本机,因为我正在使用但为什么仍然得到错误检查文档链接(https://facebook.github.io/react-native/docs/props

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

    class UserInformation extends Component(){
      render(){
        return(
          <View>
            <Text>{this.props.Name}</Text>
            <Text>{this.props.Status}</Text>
          </View>
        );
      }

}

function App() {

  return (
    <View style={styles.container}>

       <UserInformation Name='Hamza' Status='Single' />
       <UserInformation Name='Shahwar' Status='Comitted' />
    </View>
  );
}

export default App;

错误:TypeError:无法设置未定义或空引用的属性“props”

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    您不能extend 功能组件。相反,在我看来,这是一个拼写错误:

    class UserInformation extends Component{
                                //--------^^-------remove ()
    

    【讨论】:

      【解决方案2】:

      如果您使用的是类组件,则需要更改

      class UserInformation extends Component(){

      class UserInformation extends React.Component{

      并添加

      constructor(props) {
       super(props) // <---- this is the part which give you this.props
      }
      

      但是……我建议你使用像 App() 这样的函数组件

      const UserInformation = (props) => {
              return(
                <View>
                  <Text>{props.Name}</Text>
                  <Text>{props.Status}</Text>
                </View>
              );
      }
      

      【讨论】:

      • 如果您使用类组件,您可以使用组件,因为上面的示例使用组件导入。导入 React, {Component} from 'react';
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2019-01-31
      • 2019-01-11
      • 2015-03-30
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多