React组件应该抛出未定义的错误,但它没有

时间:2018-03-14 08:42:52

标签: javascript reactjs react-native

重现的步骤

目前我正在开发一个react本机项目,我的一个组件看起来像这样:

import React from 'react';
import { StyleSheet, TouchableOpacity, ScrollView } from 'react-native';
import { Box, Text, RowBox, Card, CardTitle } from '../../../component';
import { Colors, Size } from '../../../config';
import Echarts from 'native-echarts';
import {connect} from 'react-redux';

class HistoryBuy extends React.PureComponent {

  render () {
    console.log('0', this.props.history);
    const { abnormal } = this.props.history;
    console.log('1', this.props.history);
    const option = abnormal.amount;
    console.log('2', this.props.history);
    return (
      <Card title="SOME TITLE">
        <Box style={{ height: Size.px(560) }} padding={Size.px(30)}>
          <Echarts option={option} height={Size.px(500)} width={Size.DEVICE_WIDTH - Size.px(60)}/>
        </Box>
      </Card>
    );
  }
}

const mapStateToProps = state => ({
  history: state.purchase.history,
});

export default connect(mapStateToProps)(HistoryBuy);

它使用的唯一prop history以初始状态存储在redux中:

history: {
    abnormal: {
      amount: [],
      count: []
    },
    normal: {
      amount: [],
      count: []
    },
    time: []
  }

我突然将history更改为{}

有什么期望?

抛出未定义的错误。由于this.props.history更改为{}this.props.history.abnormal未定义,因此const option = abnormal.amount;会引发错误。

发生了什么事?

它不会抛出错误,只需在chrome中打印控制台。

此外,它控制台5次,而不是6次,似乎const option = abnormal.amount;之后的所有内容都无法访问。 enter image description here

版本

  • &#34;反应&#34;:&#34; 16.0.0-alpha.12&#34;
  • &#34; react-native&#34;:&#34; 0.48.2&#34;

0 个答案:

没有答案