CrocoDillon / universal-react-redux-boilerplate

Step by step creation of a Universal React + Redux Boilerplate
Other
176 stars 33 forks source link

Preload data on non-routes component #26

Closed devmarwen closed 7 years ago

devmarwen commented 7 years ago

First thanks for the awesome work :100: I saw that you used an onEnter hook to fetch data on components that are used as pages/routes.

What if we wanted to fetch data on non pages/routes components, how to change this bit of code to achieve that:

// On server we want to fetch all data for the current route before rendering
const fetchData = store => (nextState, replace, callback) => {
  Promise.all(
    nextState.routes.map(route => {
      if (route.component.onEnter) {
        return route.component.onEnter(store, nextState)
      }
    })
  ).then(() => callback())
}

Thanks!

CrocoDillon commented 7 years ago

That's tricky, with React Router and co you only get the matching routes... not their descendants.

Can't you change your component tree and put the components that need data in the route config? (which just updated to RRv4 btw, so expect some changes)