Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Upgrade react-router-dom to v5.0 #237

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"history": "^4.7.2",
"prop-types": "^15.6.2",
"react-router-dom": "^4.3.1"
"react-router-dom": "^5.0.0"
},
"peerDependencies": {
"fusion-core": "^1.10.1",
Expand Down
77 changes: 51 additions & 26 deletions src/modules/Redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import * as React from 'react';
import PropTypes from 'prop-types';

// $FlowFixMe
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe what this $FlowFixMe is fixing? I usually add the error from Flow next to the $FlowFixMe. You can also just reply to this if you don't feel like adding that info to the code, because I'm interested in why we need it.

import {__RouterContext as RouterContext} from 'react-router-dom';

import type {LocationShapeType, RedirectType} from '../types.js';

type PropsType = {|
Expand All @@ -20,41 +23,52 @@ type PropsType = {|
code?: number | string,
children?: React.Node,
|};

type HistoryContextType = {
push: (el: string | LocationShapeType) => void,
replace: (el: string | LocationShapeType) => void,
};

type StaticContextType = {
setCode: (code: number) => void,
redirect: (el: string | LocationShapeType) => void,
};

type ContextType = {
router: {
history: {
push: (el: string | LocationShapeType) => void,
replace: (el: string | LocationShapeType) => void,
},
staticContext?: {
setCode: (code: number) => void,
redirect: (el: string | LocationShapeType) => void,
},
router?: {
staticContext?: StaticContextType,
},
};
export class Redirect extends React.Component<PropsType> {
context: ContextType;

constructor(props: PropsType, context: ContextType) {
super(props, context);
if (this.isStatic(context)) this.perform();
class Lifecycle extends React.Component<{
onConstruct?: () => void,
onMount?: () => void,
}> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you assign the object type to a type variable that describes what it represents?

React.Component<{	  
    onConstruct?: () => void,
    onMount?: () => void,
}>

// changes to 

type MyCoolType = {
    onConstruct?: () => void,
    onMount?: () => void,
};

React.Component<MyCoolType>

constructor(props) {
super(props);
if (this.props.onConstruct) this.props.onConstruct.call(this, this);
}
componentDidMount() {
if (this.props.onMount) this.props.onMount.call(this, this);
}
render() {
return null;
}
}

export class Redirect extends React.Component<PropsType> {
context: ContextType;

static defaultProps = {
push: false,
code: 307,
};

componentDidMount() {
if (!this.isStatic()) this.perform();
}

isStatic(context: ContextType = this.context): boolean {
return !!(context && context.router && context.router.staticContext);
}

perform() {
const {history, staticContext} = this.context.router;
perform(history: HistoryContextType, staticContext: ?StaticContextType) {
const {push, to, code} = this.props;

if (__NODE__ && staticContext) {
Expand All @@ -71,18 +85,29 @@ export class Redirect extends React.Component<PropsType> {
}

render() {
return null;
return (
<RouterContext.Consumer>
{context => {
const history = context.history;
const staticContext =
this.context.router && this.context.router.staticContext;
const perform = () => this.perform(history, staticContext);

const props = this.isStatic()
? {onConstruct: perform}
: {onMount: perform};

return <Lifecycle {...props} />;
}}
</RouterContext.Consumer>
);
}
}

Redirect.contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
push: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
}).isRequired,
staticContext: PropTypes.object,
}).isRequired,
}),
};

// Sanity type checking
Expand Down
Loading