Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Undefined不是对象(\u this.state)_Android_Reactjs_React Native - Fatal编程技术网

Android Undefined不是对象(\u this.state)

Android Undefined不是对象(\u this.state),android,reactjs,react-native,Android,Reactjs,React Native,我对react native还是一个新手,只在状态设置为true时才尝试渲染视图。然而,尽管我一直在努力,但最终还是会出现以下错误: Undefined is not an object (evaluating '_this.state') 守则: "use strict" import React, { Component } from 'react' import * as Animatable from './animations' import Icon from 'react-nat

我对react native还是一个新手,只在状态设置为true时才尝试渲染视图。然而,尽管我一直在努力,但最终还是会出现以下错误:

Undefined is not an object (evaluating '_this.state')
守则:

"use strict"
import React, { Component } from 'react'
import * as Animatable from './animations'
import Icon from 'react-native-vector-icons/FontAwesome'
import renderIf from './libs/renderif'
import { Text, TouchableOpacity, View, StyleSheet, Navigator } from 'react-native'
var MyCustomComponent = Animatable.createAnimatableComponent(MyCustomComponent);

const Ic = ({ name, style, onPress }) => (
    <TouchableOpacity onPress={onPress}>
        <Icon name={name} style={style} />
    </TouchableOpacity>
)

const Btn = ({label, onPress}) => (
  <TouchableOpacity style={styles.btn} onPress={onPress}>
    <Text style={styles.btnText}>
      {label}
    </Text>
  </TouchableOpacity>
)

const Dashboard = ({ navigator }) => (
    <View style={styles.container}>
        <Text style={styles.text}>
          In Dashboard
        </Text>
        <Btn label="Let's go back" onPress={() => navigator.pop()} />
    </View>
)

const Home = () => (    // this is where I am trying to access the state
    <View>
        <View style={styles.navbarContainer}>
            <View style={styles.navbar}>
                <View style={styles.flexes}>
                    <Ic 
                      name="bars"
                      style={styles.iconLeft}
                      onPress={() => this.setState({isOpen: true})}
                    />
                </View>
                <View style={styles.flexes}>
                    <Text style={styles.navbarTitle}>
                        title
                    </Text>
                </View>
                <View style={styles.rightBtn}>
                    <Ic 
                      name="envelope"
                      style={styles.iconRight}
                      onPress={() => alert("envelope pressed")}
                     />
                </View>
            </View>
        </View>
        {renderIf(this.state.isOpen)(
            <View style={{marginTop: 40}}>
                <Text>hi</Text>
            </View>
        )}
    </View>
)

export default class App extends Component {
    constructor(props){
        super(props)
        this.state = {
            isOpen: false
        }
    }

    renderScene(route, navigator) {
        return(
            <route.component
                navigator={navigator}
            />
        )
    }

    render() {
        return (
            <Navigator 
                renderScene={this.renderScene.bind(this)}
                initialRoute={{
                    component: Home
                }}
            />
        )
    }
}
以及主索引index.android.js:

import { AppRegistry } from 'react-native'
import App from './app';

AppRegistry.registerComponent('NavRoute', () => App);
我试图找到一个相关的问题,之前已经得到了回答,但遗憾的是,我没有找到我需要的问题。如果这个问题以前有人回答过,请告诉我这条线索,并对给您带来的不便表示最深切的歉意


提前谢谢你

您试图在功能性/无状态组件中使用状态。这是不可能的。要使用状态,请使用React组件的类版本。必须在构造函数中明确定义状态,才能在呈现函数中使用
this.state

export default class Home extends React.Component {
  constructor() {
    super();
    this.state = {
      someState: ''
    }
  }

  // rest of the code below
  // move Home to a separate file - home.jsx and import it
}

您正试图在功能性/无状态组件中使用状态。这是不可能的。要使用状态,请使用React组件的类版本。必须在构造函数中明确定义状态,才能在呈现函数中使用
this.state

export default class Home extends React.Component {
  constructor() {
    super();
    this.state = {
      someState: ''
    }
  }

  // rest of the code below
  // move Home to a separate file - home.jsx and import it
}

很抱歉问这个问题,您是否尝试过删除renderIf并使用类似的东西:
{if(this.state.isOpen){return(…)}else{/*something*/}
很抱歉问这个问题,您是否尝试过删除renderIf并使用类似的东西:
{if(this.state.isOpen){return(…)}else{/*something*/}
@vijajst我确实定义了它。向下滚动到导出默认类应用…您正在使用的主组件。状态正常。将构造函数添加到
const Home
将导致语法错误。你能编辑你的答案并告诉我如何正确定义它吗?谢谢。对我来说,问题是我没有将
super()constructor@vijajst我确实给它下了定义。向下滚动到导出默认类应用…
您正在使用的主组件。状态正常。将构造函数添加到
const Home
将导致语法错误。你能编辑你的答案并告诉我如何正确定义它吗?谢谢。对我来说,问题是我没有在构造函数中包含
super()