Using route config to load data

We will define loadBranchData to use matchRoutes from react-router-config, and the routes defined in the route configuration file to look for a route matching the incoming request URL.

mern-mediastream/server/express.js:

import { matchRoutes } from 'react-router-config' import routes from './../client/routeConfig' const loadBranchData = (location) => {  const branch = matchRoutes(routes, location)   const promises = branch.map(({ route, match }) => {    return route.loadData      ? route.loadData(branch[0].match.params)      : Promise.resolve(null)  })  return Promise.all(promises)}

If a matching route is found, then any associated loadData method will be executed to return a Promise containing the fetched data or null if there ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.