bodyno / react-starter-kit

Start your first React App. By using React, Redux, and React-Router.

Home Page:https://github.com/bodyno/react-starter-kit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

请教一个初始化复杂state的问题

JamesYuan opened this issue · comments

Hi,

我想实现一个在点击link的时候,就能够从服务器端取得一个复杂的object (nested object)。然后把这个object做为initialstate来构造组件。

如果在react-router的onEnter中实现,它会阻塞页面的刷新?。
如果放在component里的componentDidMount里做ajax fetch,它会提示state中的object is 'undefined'。

请问在现有的这个结构中,如何实现这种点击link的时候,从服务器加载数据并初始化components?

https://github.com/bodyno/react-starter-kit/blob/master/src/routes/Zen/modules/zen.js

Thanks,
James

因为是异步的,总得有个loading的过程
这个过程建议是放在跳转后的loading中

这个loading过程,你指的是componentDidMount吗?

指的是在render中loading

在componentDidMount中调ajax

等值返回后

取消loading

Got it.

另外,对于复杂object (nest object), 我可以在render()中把undefined的object直接给默认值?

const { menus=[], fees=[], amount } = this.props

你可以将默认值直接写在module目录中的initialState