Skip to content

Commit

Permalink
fix(client): make sure the route match is added to the context (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
crash7 committed May 30, 2020
1 parent d41f0ba commit 7e713c8
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/client/src/Before.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ShouldRenderProps
} from 'Before.component';
import React, { useCallback, useEffect, useReducer, useRef, useMemo, memo } from 'react';
import { withRouter, Switch, Route, type ContextRouter } from 'react-router-dom';
import { withRouter, Switch, Route, type ContextRouter, matchPath } from 'react-router-dom';
import { compose, concat, has, head, ifElse, last, propOr, prop, split } from 'ramda';
import { getQueryString, findRouteByPathname } from './utils';

Expand Down Expand Up @@ -42,10 +42,7 @@ const getSearch: (pathname: string | LocationType) => string = ifElse(
* @func
* @retuns {string} the generated key
**/
const createLocationKey = () =>
Math.random()
.toString(36)
.substr(2, 5);
const createLocationKey = () => Math.random().toString(36).substr(2, 5);

/**
* Inject querystring into the react-router match object
Expand Down Expand Up @@ -126,18 +123,19 @@ export function Before(props: BeforeComponentWithRouterProps) {
route,
{
...props,
location: { pathname: route.path, hash: '', search, state },
match: matchPath(path, route),
location: { pathname: path, hash: '', search, state },
query: getQueryString({ search })
},
props => {
if (!interrupt.current) {
initialProps.current[route.path] = props;
initialProps.current[path] = props;
dispatch({
type: 'update-location',
location: {
hash: '',
key: `before-${createLocationKey()}`,
pathname: route.path,
pathname: path,
search,
state
}
Expand All @@ -160,9 +158,14 @@ export function Before(props: BeforeComponentWithRouterProps) {
if (route) {
fetchInitialProps(
route,
{ ...props, location, query: getQueryString(location) },
{
...props,
match: matchPath(location.pathname, route),
location,
query: getQueryString(location)
},
props => {
initialProps.current[route.path] = props;
initialProps.current[location.pathname] = props;
dispatch({ type: 'update-location', location });
interrupt.current = false;
}
Expand Down

0 comments on commit 7e713c8

Please sign in to comment.