При загрузке страницы, я отправляю внутри своего 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}
из уравнения, то процесс продолжится и действие будет отправлено. Тогда я могу назвать уравнение w/HMR и hooray !! Данные есть ...
Итак, мой вопрос и благодарность за то, что он со мной связан, заключается в том, как я могу его получить, чтобы мой немой компонент ожидал, чтобы попытаться получить доступ к этим свойствам в состоянии до тех пор, пока ПОСЛЕ моих начальных отправленных действий не произойдет ?
Есть ли способ сделать 'connect' метода этого использования React-Redux в? Я не могу быть единственным, кто имеет дело с этим, что заставляет меня думать, что я просто устанавливаю это неправильно. Тот факт, что я должен использовать другие зависимости, которые ... – Nappstir