简体   繁体   English

React&Redux应用程序设计

[英]React & Redux Application Design

I'm new to redux and writing a frontend application that holds a global state which is retrieved on startup via ajax and then updated via poll/push as new data is retrieved. 我是Redux的新手,编写了一个拥有全局状态的前端应用程序,该状态在启动时通过Ajax进行检索,然后在获取新数据时通过poll / push进行更新。 On navigation to each url there's an actor which dispatches to an action which then calls a reducer that overwrites part of the viewState object with a filtered view of this globalState . 在导航到每个url时,有一个actor调度到一个动作,然后该动作调用一个reducer,该viewState用此globalState的过滤视图覆盖viewState对象的一部分。 eg navigation to the url /items/filter/a would adjust the viewState to the following: 例如,导航到URL /items/filter/a会将viewState调整为以下内容:

state {
     viewState: {
          items: [{id: 1, value: 'a'}]
     }
     globalState: {
          items: [{id: 1, value: 'a'}, 
                  {id: 2, value: 'b'}, 
                  {id: 3, value: 'c']}
     }
}

After this step then ReactDOM.render() is called via another actor and all the components re-render with the updated values in viewState (this isn't my code its straight from the unicorn starter pack) 在此步骤之后,然后通过另一个actor调用ReactDOM.render()并使用viewState的更新值重新呈现所有组件(这不是我的代码,直接来自unicorn入门包)

I'm wondering a) if this is a sensible way to go about structuring an application of this type. 我想知道a)这是否是构造此类应用程序的明智方法。 and b) what is the best way of initializing the globalState variable on application bootup and then listening for updates (which would potentially then update the viewState if that object with that id is currently in the viewState ) b)在应用程序启动时初始化globalState变量然后侦听更新的最佳方法是什么(如果具有该ID的对象当前在viewState则可能会更新viewState

I don't have experience with Redux, since we used plain-vanilla Flux in our project, but we has a similar scenario, and our solution might fit your scenario as well. 我没有使用Redux的经验,因为我们在项目中使用了普通香草Flux,但是我们有一个类似的方案,我们的解决方案也可能适合您的方案。

Generally I think this is a good strategy for segmenting your global state. 通常,我认为这是分割全球状态的好策略。 There are also libraries that do this (like Facebook Relay for example), that might give you this functionality and more out-of-the-box. 也有一些库可以做到这一点(例如Facebook Relay ),它可以为您提供此功能以及更多现成的功能。

About the rendering of initial state, what we did is set up all of our components to handle an empty data set well, ie if they receive no data (on initial render) they just render empty div s and a loader. 关于初始状态的呈现,我们所做的就是将所有组件设置为很好地处理空数据集,即,如果它们在初始呈现时未接收到任何数据,则仅呈现空div和加载器。 Then, when all the data has been retrieved from the server (we use promises for synchronization using the Q library) we fire a BeginRender action, which causes all the stores that are now filled with data to fire a change event. 然后,从服务器检索到所有数据后(使用Q库使用BeginRender进行同步),我们将BeginRender操作,该操作将导致所有现在已填充数据的存储区触发change事件。 The components subscribed to the stores receive the event and call the store methods to retrieve the data. 订阅商店的组件将接收事件,并调用商店方法以检索数据。 Once they receive the new data, the component state is updated and the components re-render with the data. 一旦他们接收到新数据,组件状态就会更新,并且组件会用数据重新渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM