{"size": 1297, "ext": "js", "max_stars_repo_path": "imports/ui/Lazy/LazyRoute.js", "max_stars_repo_name": "pixelbandito/election", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "imports/ui/Lazy/LazyRoute.js", "max_issues_repo_name": "pixelbandito/election", "max_issues_repo_issues_event_min_datetime": "2020-09-06T21:07:44.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-30T21:49:11.000Z", "max_forks_repo_path": "imports/ui/Lazy/LazyRoute.js", "max_forks_repo_name": "pixelbandito/election", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 22.7543859649, "max_line_length": 95, "alphanum_fraction": 0.633770239}
import React, { useEffect } from 'react';
import { Route } from 'react-router-dom';
import RoutesToLoadersContext, { SetRoutesToLoadersContext } from './RoutesToLoadersContext';

const LazyRoute = ({
  fetchData,
  routesToLoadersMap,
  setRoutesToLoadersMap,
  exact,
  path,
  sensitive,
  strict,
  ...passedProps
}) => {
  useEffect(() => {
    if (!fetchData) { return; }

    const routeKey = [JSON.stringify({
      exact,
      path,
      sensitive,
      strict,
    })];

    if (routesToLoadersMap[routeKey] && routesToLoadersMap[routeKey].fetchData === fetchData) {
      return;
    }

    setRoutesToLoadersMap({
      ...routesToLoadersMap,
      [routeKey]: {
        fetchData,
      }
    });
  }, [exact, fetchData, path, routesToLoadersMap, sensitive, setRoutesToLoadersMap, strict]);

  return <Route {...passedProps} />;
};

const LazyRouteWithContext = props => (
  <RoutesToLoadersContext.Consumer>
    {rtlContext =>
      <SetRoutesToLoadersContext.Consumer>
        {srtlContext =>
          <LazyRoute
            routesToLoadersMap={rtlContext}
            setRoutesToLoadersMap={srtlContext}
            {...props}
          />
        }
      </SetRoutesToLoadersContext.Consumer>
    }
  </RoutesToLoadersContext.Consumer>
);

export default LazyRouteWithContext;
