Reactjs 反应重复。如何在第一次应用程序渲染时获取更新的存储状态

Reactjs 反应重复。如何在第一次应用程序渲染时获取更新的存储状态,reactjs,webpack,redux,react-redux,webpack-2,Reactjs,Webpack,Redux,React Redux,Webpack 2,我很好奇渲染组件的好做法是什么,以及如何重新渲染它以获得更新的存储 例如,在当前时刻,项目有一个用于侦听react路由器并存储当前位置的存储 商店 export default (initialState = {slidesData}) => { const store = createStore( makeRootReducer(), initialState ) store.asyncReducers = {} store.unsubscribeHi

我很好奇渲染组件的好做法是什么,以及如何重新渲染它以获得更新的存储

例如,在当前时刻,项目有一个用于侦听react路由器并存储当前位置的存储

商店

export default (initialState = {slidesData}) => {

  const store = createStore(
    makeRootReducer(),
    initialState
  )

  store.asyncReducers = {}
  store.unsubscribeHistory = hashHistory.listen(updateLocation(store));

  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const reducers = require('./reducers').default
      store.dispatch(reducers(store.asyncReducers))
    })
  }

  return store
}
位置减速器

export const LOCATION_CHANGE = 'LOCATION_CHANGE';

const hash = '/#/',
      browser = '/';

// Action
export function locationChange (location = hash) {
  return {
    type: LOCATION_CHANGE,
    payload: location
  }
}


// Action creator
export const updateLocation = ({ dispatch }) => {
  return (nextLocation) => dispatch(locationChange(nextLocation))
}


// Reducer
const initialState = null;
export default function locationReducer (state = initialState, action) {
  return action.type === LOCATION_CHANGE
    ? action.payload
    : state
}
因此,在第一个应用程序上加载订阅存储的组件接收
initialState=null
,当第一个路由发生更改时,存储更新,组件现在接收
“/current route”

在订阅的组件接收到
null
之前,您是否可以分享您的想法,以获得
“当前路由”



或者,如果组件接收到
null
并触发其重新渲染器以显示更新的存储,如何处理组件渲染?

我知道的唯一方法是在调度任何操作(如路由更改或其他操作)之前将其置于reducer的默认状态对象中

i、 e.
const reducer=(state=,action)=>{…

因此,在您的情况下,我建议从
窗口
对象抓取当前路由,而不是使用
null
初始化:

// Reducer
const initialState = window.location.pathname;

我所知道的在调度任何操作(如路由更改或其他操作)之前使某些内容进入状态的唯一方法是将其置于reducer的默认状态对象中

i、 e.
const reducer=(state=,action)=>{…

因此,在您的情况下,我建议从
窗口
对象抓取当前路由,而不是使用
null
初始化:

// Reducer
const initialState = window.location.pathname;

我不确定我是否正确理解了您的问题,但我显示“正在加载…”在连接的组件从存储区接收数据之前。@IhorBurlachenko,在这种情况下,它实际上从存储区接收数据,它等于null,因此它呈现加载,并永远保持在那里。您不能将initialState设置为应用程序启动时想要的值吗?@IhorBurlachenko我可以用另一种方法解决问题,只要curious如果还有其他选项我会看到的。我自己按照reactjs主题学习React中的其他方法:)我不确定我是否正确理解了您的问题,但我显示“加载…”在连接的组件从存储区接收数据之前。@IhorBurlachenko,在这种情况下,它实际上从存储区接收数据,它等于null,因此它呈现加载,并永远保持在那里。您不能将initialState设置为应用程序启动时想要的值吗?@IhorBurlachenko我可以用另一种方法解决问题,只要curi如果有其他选择,请点击此处。我自己也会跟随React的主题,以便了解React中的其他方法:)