Last active
January 24, 2017 17:14
-
-
Save taion/66b6ce17b601568d7ad3321e3dda8b8d to your computer and use it in GitHub Desktop.
React Router data fetching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define your components like: | |
class MyComponent extends React.Component { | |
static fetchData = (params) => { | |
// return an action here. | |
}; | |
/* ... */ | |
} | |
function fetchComponentData(component, store, params) { | |
if (component.fetchData) { | |
store.dispatch(component.fetchData(params)); | |
}; | |
} | |
export default store => ({ | |
createRouterContext: (child, { components, params }) => { | |
for (const component of components) { | |
if (!component) { | |
continue; | |
} | |
if (typeof component === 'object') { | |
Object.keys(component).forEach(key => { | |
fetchComponentData(component[key], store, params); | |
}); | |
continue; | |
} | |
fetchComponentData(component, store, params); | |
} | |
return child; | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define your routes like: | |
const myRoute = ( | |
<Route | |
component={myComponent} | |
fetchData={params => /* action */} | |
/> | |
); | |
export default store => ({ | |
createRouterContext: (child, { routes, params }) => { | |
routes | |
.map(route => route.fetchData) | |
.filter(fetchData => fetchData) | |
.forEach(fetchData => store.dispatch(fetchData(params))); | |
return child; | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ReactDOM.render( | |
<Router | |
history={history} | |
routes={routes} | |
render={applyRouterMiddleware(useFetchData(store))} | |
/>, | |
container | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discovered a bug with the above. Here's a more complete resolution: