[英]React Native/Redux - Can't access Redux store
I'm trying to access two parts of my Redux store within a React Native component that I'm working on and for some reason, probably something simple, I can't get things working. 我试图在我正在使用的React Native组件中访问Redux存储的两个部分,由于某种原因,可能是一些简单的事情,我无法正常工作。
I'm passing the orderID
as a property to the component and then I want to retrieve the appropriate order details from the Redux store: orders[orderID]
but when trying to assign the local variable: 我将
orderID
作为属性传递给组件,然后我想从Redux存储中检索适当的订单详细信息: orders[orderID]
但是在尝试分配局部变量时:
const order = this.props.orders[orderID];
I'm getting the error: Cannot read property 'orders' of undefined
, ie: for some reason the orders
part of the Redux store appears to not have connect
ed to the component props. 我收到错误消息:
Cannot read property 'orders' of undefined
,即:由于某种原因,Redux存储的orders
部分似乎未connect
至组件prop。
Code is as follows: 代码如下:
import React from 'react';
import { connect } from 'react-redux';
import {
View,
Text
} from 'react-native';
import Status from '../Status';
import Card from './Card';
import CardSection from './CardSection';
const OrderDetail = ({ orderID }) => {
const order = this.props.orders[orderID];
const {
id,
status,
gross_price,
currency_symbol,
bookings
} = order;
return (
<Card>
<CardSection>
<View style={styles.headerContentStyle}>
<View style={styles.bookingIdHeaderContainerStyle}>
<Text style={styles.headerTextStyle}>Booking #{id}</Text>
</View>
<View style={styles.grossPriceHeaderContainerStyle}>
<Text style={styles.headerTextStyle}>{currency_symbol}{gross_price}</Text>
</View>
<View style={styles.statusHeaderContainerStyle}>
<Status status={status} />
</View>
</View>
</CardSection>
<CardSection>
<View style={styles.orderListContentStyle}>
<Text>Booking #1234</Text>
</View>
</CardSection>
</Card>
);
};
const styles = {
headerContentStyle: {
flex: 1,
flexDirection: 'row',
alignItems: 'center'
},
headerTextStyle: {
fontSize: 18
},
bookingIdHeaderContainerStyle: {
flex: 5
},
grossPriceHeaderContainerStyle: {
flex: 2
},
statusHeaderContainerStyle: {
flex: 2
},
orderListContentStyle: {
}
};
const mapStateToProps = state => {
return ({
orders: state.orders,
bookings: state.bookings
});
};
export default connect(mapStateToProps)(OrderDetail);
Any suggestions? 有什么建议么?
OrderDetail
is a stateless functional component, and functional components don't have a this
keyword. OrderDetail
是无状态功能组件,并且功能组件没有this
关键字。
May be this is what you wanted: 可能是您想要的:
const OrderDetail = (props) => {
const order = props.orders[props.orderID];
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.