动作分派后渲染组件

IT技术 javascript reactjs redux
2021-05-20 03:29:23

在页面加载时,我在我的 内分派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} />
  />

再次,相信我有适当的mapStateToPropsmapDispatchToProps等。然后我会在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 和万岁的方程!!数据在那里...

所以我的问题,感谢您的耐心,我怎样才能得到它,以便我的哑组件等待尝试访问状态上的这些属性,直到我的初始调度操作发生之后?

3个回答

如果 getWeatherReports 请求是由支持Promise而不是回调来通知您成功响应的东西发出的(即类似axios 的东西),那么您可以尝试使用像redux-promise这样的中间件

什么终极版,Promise会做的是拦截调度动作和转发动作到商店的用户若Promise一直没有解决停止。

一旦您获得成功的响应并且Promise得到解决,中间件就会创建一个新的操作,其操作类型与原始操作类型相同,但其有效负载包含您需要的数据。

您可以按如下方式使用它:

import { createStore, applyMiddleware } from 'redux';
import promise from 'redux-promise'; 

const store = configureStore();
store.dispatch(loadBlogs());
...
...
store.dispatch(getWeatherReports());

const storeWithMiddleWare = applyMiddleware(promise)(store);

似乎store.dispatch(getWeatherReports())是一个异步 API 调用。因此,在 DOM 中呈现组件之前,您需要等待响应。

解决方案:使用redux-connect

关键点:

  • 它允许您请求异步数据,将它们存储在 redux 状态并将它们连接到您的 React 组件。

  • 这个包由两部分组成:一部分允许你延迟容器渲染,直到发生一些异步操作。另一个将您的数据存储到 redux 状态并将您加载的数据连接到您的容器

您的组件在Promise完成之前不会呈现。

主要优点是:

  • 为 CSR(客户端渲染)和 SSR(服务器端渲染)配置简单而优雅。

  • 您可以在自己的减速器中对获取数据加载或加载成功等操作做出react

  • 您可以创建自己的中间件来处理 Redux Async Connect 操作
  • 您可以连接到其他任何地方加载的数据,只需使用简单的 redux @connect
  • 最后,您可以使用 Redux Dev Tools 调试和查看您的数据

  • 它还与React Router集成以防止路由转换,直到数据加载。

确保您不要忘记配置的 4 个步骤(此处描述):

  1. 连接你的数据,类似于 react-redux @connect
  2. 访问数据作为props
  3. 连接 redux 异步减速器
  4. Router使用 ReduxAsyncConnect 中间件渲染

所以在DashboardPage假设 getWeatherReports() 会返回一个Promise)中,我们可以做的是:

如果你有使用装饰器的 babel 预设:

import { asyncConnect } from 'redux-connect';

@asyncConnect([{
  key: 'lunch',
  promise: ({ store: { dispatch }, params }) => dispatch(getWeatherReports())
}])
export default class DashboardPage extends React.Component {
 ...
}

否则:

class DashboardPage extends React.Component {
 ...
}


// sorry for the long name ;)
const asyncConnectedDashboardPage = asyncConnect([{
  key: 'lunch',
  promise: store.dispatch(getWeatherReports())
}])

export default asyncConnectedDashboardPage;

使用多重Promise,

@asyncConnect([{
  promise: ({ store: { dispatch, getState } }) => {
    const promises = [];
    const state = getState();
    if (/* some condition */) {
      promises.push(dispatch(loadBlogs()))
    }

    if (/* some other condition */) {
      promises.push(dispatch(loadUsers()))
    }

    promises.push(dispatch(getWeatherReports()))
    return Promise.all(promises);
  },
}])

reports[0]在尝试访问之前检查是否存在.main如果它undefined然后显示加载微调器或其他东西......

 if (!reports[0]) return <div>Loading...</div>

 return (
   // your component like normal.
 )