TypeError: undefined is not an object (evaluating '_this3.state.bind')

TypeError: undefined is not an object (evaluating '_this3.state.bind')

App.js代码:

import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';



class HomeScreen extends React.Component {
  constructor(props){
    super(props);
    this.state={count:0};
    this.incrementCount=this.incrementCount.bind(this)
  }

  incrementCount(){
    this.setState({
      count: this.state.count + 1
    })
  }

  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text style={styles.homeScreen}>Home Screen</Text>
        <Button
          title="Go to Details"
          onPress={() => {
            this.incrementCount();
            this.props.navigation.navigate('Details');           
          }}
        />
      </View>
    );
  }
}

class DetailsScreen extends React.Component {

  constructor(props){
    super(props);
    this.state=this.state.bind(this)
    this.incrementCount=this.incrementCount.bind(this)
  }
  render() {
    return (

      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Hello </Text>
      </View>
    );
  }
}

const AppNavigator = createStackNavigator(
  {
    Home: HomeScreen,
    Details: DetailsScreen,
  },
  {
    initialRouteName: 'Home',
  }
);

const styles= StyleSheet.create({
    homeScreen:{

    }
});

export default createAppContainer(AppNavigator);

我想在用户每次转到详细信息(第二页)页面时递增一个数字 (0)。增加后的数字应该显示在详情页(第二页)

我是react native的初学者,我不知道如何在不同的地方使用状态类。请解释状态的概念以及解决方案。

您必须将您的 count 作为道具发送给您的 DetailsPage。所以在代码中它看起来像这样:

<Button
       title="Go to Details"
       onPress={() => {
            this.incrementCount();
            this.props.navigation.navigate('Details',{count:this.state.count});           
       }}/>

并且在您的 DetailsS​​creen 中,您必须像这样访问它:

class DetailsScreen extends React.Component {

  constructor(props){
    super(props);
    //Remove these lines this is causing error and this is wrong
    //this.state=this.state.bind(this)
    //this.incrementCount=this.incrementCount.bind(this)
  }
  render() {
    let count = this.props.navigation.getParam('count')
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>You were here {count} times </Text>
      </View>
    );
  }
}