Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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
Javascript TypeError:undefined不是对象(正在计算';_this3.state.bind';)_Javascript_React Native_Navigation_State_Setstate - Fatal编程技术网

Javascript TypeError:undefined不是对象(正在计算';_this3.state.bind';)

Javascript TypeError:undefined不是对象(正在计算';_this3.state.bind';),javascript,react-native,navigation,state,setstate,Javascript,React Native,Navigation,State,Setstate,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 Reac

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);
从“React”导入React;
从“react native”导入{视图、文本、按钮、样式表};
从“react navigation”导入{createAppContainer};
从“反应导航堆栈”导入{createStackNavigator};
类主屏幕扩展了React.Component{
建造师(道具){
超级(道具);
this.state={count:0};
this.incrementCount=this.incrementCount.bind(this)
}
递增计数(){
这是我的国家({
计数:this.state.count+1
})
}
render(){
返回(
主屏幕
{
这是.incrementCount();
this.props.navigation.navigate('Details');
}}
/>
);
}
}
类DetailsScreen扩展了React.Component{
建造师(道具){
超级(道具);
this.state=this.state.bind(this)
this.incrementCount=this.incrementCount.bind(this)
}
render(){
返回(
你好
);
}
}
const AppNavigator=createStackNavigator(
{
主页:主屏幕,
详情:详情屏幕,
},
{
initialRouteName:“主页”,
}
);
const styles=StyleSheet.create({
主屏幕:{
}
});
导出默认createAppContainer(AppNavigator);
我想在用户每次进入详细信息(第二页)页面时增加一个数字(0)。递增的数字应显示在详细信息(第二页)页面上


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

您必须将您的
计数作为道具发送到
详细信息页面。因此,在代码中,它将如下所示:

<Button
       title="Go to Details"
       onPress={() => {
            this.incrementCount();
            this.props.navigation.navigate('Details',{count:this.state.count});           
       }}/>
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>
    );
  }
}
{
这是.incrementCount();
this.props.navigation.navigate('Details',{count:this.state.count});
}}/>
在您的DetailsScreen中,您必须按如下方式访问它:

<Button
       title="Go to Details"
       onPress={() => {
            this.incrementCount();
            this.props.navigation.navigate('Details',{count:this.state.count});           
       }}/>
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>
    );
  }
}
class DetailsScreen扩展了React.Component{
建造师(道具){
超级(道具);
//删除这些行这会导致错误,这是错误的
//this.state=this.state.bind(this)
//this.incrementCount=this.incrementCount.bind(this)
}
render(){
让count=this.props.navigation.getParam('count')
返回(
你在这里{count}次
);
}
}