我有以下行爲者:地理位置 - 不能得到經緯度反應,和/終極版應用
export const getLocation =() => {
const geolocation = navigator.geolocation;
const location = new Promise((resolve, reject) => {
if (!geolocation) {
reject(new Error('Not Supported'));
}
geolocation.getCurrentPosition((position) => {
resolve(position);
},() => {
reject (new Error('Permission denied'));
});
});
return {
type: GET_LOCATION,
payload: location
}
};
而對於GET_LOCATION
型以下減速器:
case GET_LOCATION: {
return {
...state,
location: {
latitude: action.location.coords.latitude,
longitude: action.location.coords.latitude,
}
}
}
我嘗試使用這個數據在我的組件像這樣:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getLocation } from '../actions';
import { bindActionCreators } from 'redux';
class UserLocation extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.getLocation();
}
render() {
return (
<div>
<div><span>latitude:</span>{this.props.location.latitude}
</div>
<div><span>longitude:</span>{this.props.location.longitude} </div>
</div>
);
}
}
const mapDispatchToProps = dispatch => {
return bindActionCreators({ getLocation }, dispatch)
}
export default connect(null, mapDispatchToProps)(UserLocation);
但是每當我加載這個組件我得到TypeError: Cannot read property 'latitude' of undefined
你能指點我哪裏錯了嗎?