erikras / react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
MIT License
11.99k stars 2.5k forks source link

Loading dynamic content from routes #738

Open AndreasKalva opened 8 years ago

AndreasKalva commented 8 years ago

I am new to react and redux. What is the best practice for loading content from the API for routes e.g. /cms/page1

Where I want to display the content for page1. I've already setup the API-server example to serve content for /api/cms/:pagename.

Do you suggest to create a component for that content or can I just use the route container for serving it? How can I pass the url param :pagename. What's a good approach?

trueter commented 8 years ago

Hey Andreas

Going with /api/cms/:pagename is not going to work with the default setup of the api server as discussed here. Unless you have custom routing set up I recommend sticking to the boilerplate way of doing api calls which would mean:

// redux/modules/__.js
export function loadPage ( n: number ) {
    return {
        types: [LOAD_PAGE, LOAD_PAGE_SUCCESS, LOAD_PAGE_FAIL],
        promise: (client) => client.get('/loadPage?page=' + n )
    };
}
// api/actions/loadPage.js
export default function loadPage( req ) {
  const pageNumber = req.query.page
  return makeDatabaseCallThatGetsSpecificPageAndReturnsPromise( pageNumber )
}

If you do that, don't forget to add loadPage to api/actions/index.js.