react native TypeError: TypeError: undefined is not an object (evaluating'_this.state.cartAmount')

阿兹哈尔

在App.js如果我分配this.state.cartAmounttabBarComponent ......它给错误

TypeError: TypeError: undefined is not an object (evaluating'_this.state.cartAmount')

在此处输入图片说明

如果我将tabBarComponent设置

{(this.state != null) ? this.state.cartAmount : '0.0'}

tabBarComponent 中点击TouchableOpacity Home即使我编写箭头函数也会出错

HandleTabPressOne = () => {
    this.setState({ cartAmount: '12.20' });
}

我认为构造函数存在一些问题,请指导

在此处输入图片说明

const TabNavRoutes = createBottomTabNavigator({
    Tab1: {screen: HomeScreenStack},
    Tab2: {screen: Screen2},
    Tab3: {screen: Screen3},
    Tab4: {screen: Screen4}
   },
   {
    tabBarComponent:({navigation}) => (
      <View style={{flex: 0.1, backgroundColor: '#FF0000', borderColor: '#FF0000', borderWidth: 1}}>
                <View style={{flexDirection:'row', justifyContent: 'space-around', alignItems: 'center', paddingTop: 5}}>

                    <TouchableOpacity onPress={() => this.HandleTabPressOne.bind() } style={{alignItems: 'center', marginLeft: 5}}>
                        <Text style={styles.textFAB}>Home</Text>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={() => navigation.navigate('Tab2')}style={{alignItems: 'center'}}>

                        <Text style={styles.textFAB}>History</Text>
                    </TouchableOpacity>
                    <View style={styles.cartFAB}>
                        <MaterialIcons name="shopping-basket" size={24} color="#FFFFFF" style={{marginTop: 10}} />
                        <Text style={styles.totalCart}>{ this.state.cartAmount} €</Text>
                    </View>
                </View>
            </View>
   )});

const AppSwitchNavigator = createSwitchNavigator({
  Welcome: { screen: splashScreen },
  Dashboard: { screen: TabNavRoutes }
});

const AppContainer = createAppContainer(AppSwitchNavigator);

export default class App extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      cartAmount: '18.50'
    }
  }

  HandleTabPressOne() {

    this.setState({ cartAmount: '12.20' });
    alert(this.state.cartAmount)
  }

  render() {
      return <AppContainer/>
  }
}
年龄关闭
() => this.HandleTabPressOne.bind()

你在这里有 2 个绑定。你应该做箭头或绑定。尝试这样的事情:

<TouchableOpacity onPress={this.HandleTabPressOne}

你的方法可以绑定箭头

HandleTabPressOne = () => {
  this.setState({ cartAmount: '12.20' });
  alert(this.state.cartAmount)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError: undefined is not an object (evaluating style.width) react native

TypeError: undefined is not an object (evaluating 'navigation.navigate') React Native

React Native: TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate') 当点击按钮时。为什么?

Expo/React Native 错误 - TypeError: undefined is not an object (evaluating 'string.toLowerCase')

TypeError: undefined is not an object (evaluating 'navigation.navigate') - React Navigation

[Vue 警告]:渲染错误:“TypeError: undefined is not an object (evaluating 'this.$store.state')”

TypeError: undefined is not an object (evaluating 'this.props.location') [ReactJS + react-router-dom V4]

React Native TypeError: TypeError: undefined is not an object (评估'this.props.data.map')

React-Native:TypeError:undefined 不是对象

TypeError:undefined不是React Native中的一个对象(评估'this.state.Gas')

TypeError:undefined不是React Native中的一个对象(评估'this.state.Gase')

TypeError : Object (...) is not a function react native stack navigation

React Native得到TypeError:undefined不是一个对象(正在评估'_this.state.apiData.items.map')

如何解决 TypeError: undefined is not an object (evaluating 'rss.items[0].title')

React Native:TypeError:this.state.schedule.map不是对象

React Native TypeError:undefined 不是对象(评估“props.getItem”)

React Native,TypeError:undefined不是一个对象,当它是

react native TypeError: this.setState is not a function.'this.setState' is undefined)

TypeError: undefined 不是 React-Native 中的对象

字典错误 - TypeError: undefined is not an object

TypeError:undefined不是带有React-router-dom的对象(评估“ undefined.state”)

如何修复“TypeError: null is not an object (evaluating 'data[index]')

第二次访问屏幕时,React Native null is not an object (evaluating'_this2.state.data') 错误

react-native-maps-TypeError:未定义不是对象(正在评估'_this.state.region')

× React: TypeError: Object(...) is not a function Stripe

TypeError:ShallowWrapper :: state(“ isOpen”)要求state不能为null或undefined。

React Native:TypeError:undefined不是一个对象(评估'_this.props.data.map')

React Native:未处理的Promise拒绝:TypeError:undefined不是对象(评估'response.json')

TypeError:undefined不是一个对象(正在评估“ navigation.push”)React Native