getComponent全部完成后的getIndexRoute

时间:2016-06-28 15:00:40

标签: javascript reactjs redux react-router

在我的react-router路由中,我使用了类似的plainRoutes:

getComponent (nextState, cb) {
    require.ensure([], (require) => {
      const CoreLayout  = require('../layouts/CoreLayout/CoreLayout').default
      const userActions = require('../store/user').actions
      store.dispatch(userActions.fetch())
      cb(null, CoreLayout)
    })
},

现在我需要州内的某些内容,因为我的IndexRoute.fetch完成之前可能未定义:

indexRoute: Default(store),

默认

import { injectReducer } from '../../store/reducers'

export default (store) => ({
  getComponent (nextState, cb) {
    const state = store.getState();
    require.ensure([], (require) => {
      const actions = require('../modules').actions
      store.dispatch(actions.fetch(state.list.selected))
      // snipped for brevity
      cb(null, Default)
    }, 'default')
  }
})

如何确保完成第一次异步提取?

1 个答案:

答案 0 :(得分:0)

如果您的异步操作返回承诺,则可以使用$(document).ready(function() { // page is now ready, initialize the calendar... $('#calendar').fullCalendar({ // put your options and callbacks here header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay' }, weekends: true, events: 'http://www.familyevent.comli.com/events.php', eventRender: function(event) { if (event.allDay === 'true') { event.allDay = true; } else { event.allDay = false; } }, eventClick: function(event) { alert("event start " + moment(event.start).format('MM/DD/YYYY hh:mm a') + " event end " + moment(event.end).format('MM/DD/YYYY hh:mm:ss')); } }); }); 。例如:

// actions.js

then

//默认

export default function fetch(id) {
   return (dispatch) => 
     fetch('/getSomething?id=' + id)
       .then(response => {
         dispatch({
           type: FETCH
           payload: response
         })
       });
}