I'm trying to figure out the best way to dynamically select a component and store based on the data my server receives. The app is based on these examples: https://github.com/yahoo/flux-examples/tree/master/fluxible-router but a little different:
- User goes to mysite.com/that/and/that (the app can't know what type of thing 'that/and/that' is, so this can't be handled in the routing).
- Server does a GET request to our CMS. The CMS returns the data, including what type of data it is (section listing, article, company profile, etc.)
Here I can dynamically select a component like so (this is in the render method of a parent component for now):
switch (this.props.documentType) { // From the CMS case 'Section': Handler = require('../section/section'); break; case 'Article': Handler = require('../article/article'); break; default: // Do nothing }
All good. But I also want to put my data in a particular store. What is the best way to do that? Some ideas that seem a little hacky to me:
In this same switch statement, dynamically execute an action to send data to the appropriate store, the 'article' component will rely on the 'article' store having data.
Send the data to the component itself and let (for example) the article component initialize the article store with that data.
- Actually put the document type in the URL, e.g. mysite.com/article/this/and/that and handle all this neatly in the routes. But I'd rather not mess up my URLs just because I couldn't work out an elegant solution :)
Thanks in advance for any ideas.