【发布时间】:2016-11-21 23:43:51
【问题描述】:
页面加载后,我在我的index.js 中调度了一个操作store.dispatch(getWeatherReports());,它会访问天气API。此操作通过redux 过程并最终将返回的数据添加到名为weatherReports 的状态属性中。此属性是一个具有空数组的对象。现在,我将粘贴代码的概述......并不是所有的代码都可以为您省去逐行的麻烦,因为从 API 输出数据不是我遇到的问题。
这是我的index.js:
import 'babel-polyfill';
import React from 'react';
import {render} from 'react-dom';
import configureStore from './store/configureStore';
import {Provider} from 'react-redux';
import {Router, browserHistory} from 'react-router';
import {StyleRoot} from 'radium';
import routes from './routes';
import {loadBlogs} from './actions/blogActions';
import {loadUsers, getActiveUser} from './actions/userActions';
import {getWeatherReports} from './actions/weatherActions';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import '../node_modules/toastr/build/toastr.min.css';
import './styles/app.scss';
const store = configureStore();
store.dispatch(loadBlogs());
store.dispatch(loadUsers());
store.dispatch(getActiveUser());
store.dispatch(getWeatherReports());
render(
<StyleRoot>
<Provider store={store}>
<Router history={browserHistory} routes={routes}/>
</Provider>
</StyleRoot>,
document.getElementById('app')
);
相信这会通过正确的过程返回数据。然后我创建了一个smart 组件,它采用这种状态并希望将其传递给一个哑组件。
class DashboardPage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
weatherReports: []
};
}
<ContentBox
title="What The Wind Blew In"
content={<WeatherReport reports={this.props.weatherReports} />
/>
再次,相信我有适当的mapStateToProps、mapDispatchToProps 等。然后我希望在dumb 组件中显示数据。 WeatherReport.js:
import React, {PropTypes} from 'react';
import styles from '../common/contentBoxStyles';
const WeatherReport = ({reports}) => {
console.log(reports);
return (
<div style={styles.body} className="row">
<div style={styles.weatherBoxContainer}>
<div className="col-sm-2 col-md-offset-1" style={styles.weatherBoxContainer.weatherCard}>
<div style={styles.weatherBoxContainer.weatherReport}>
<div style={styles.weatherBoxContainer.currentTemp}>
HERE IS MY PROBLEM!!
{reports[0].main.temp}
</div>
</div>
CA
</div>
<div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}>
<div style={styles.weatherBoxContainer.weatherReport}>
Report
</div>
UT
</div>
<div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}>
<div style={styles.weatherBoxContainer.weatherReport}>
Report
</div>
MN
</div>
<div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}>
<div style={styles.weatherBoxContainer.weatherReport}>
Report
</div>
DC
</div>
<div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}>
<div style={styles.weatherBoxContainer.weatherReport}>
Report
</div>
NY
</div>
</div>
</div>
);
};
WeatherReport.propTypes = {
reports: PropTypes.array
};
export default WeatherReport;
我的根本问题是,在我的调度操作返回数据之前。我试图通过我的dumb 组件来呈现它。因此,我在尝试访问数据时收到以下错误:Uncaught TypeError: Cannot read property 'main' of undefined(…) 执行以下操作时:reports[0].main.temp
正在发生的事情是这阻止了我的应用程序继续前进,因此store.dispatch(getWeatherReports()); 永远不会被调用。因此,如果我从等式中删除{reports[0].main.temp},则该过程将继续并且动作被调度。然后我可以调用带有 HMR 和万岁的方程式!数据就在那里……
所以我的问题是,感谢您对我的支持,我怎样才能得到它,以便我的哑组件等待尝试访问状态上的这些属性,直到在我最初调度的操作发生之后?
【问题讨论】:
标签: javascript reactjs redux