This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Upgrade react-router-dom to v5.0 #237
Open
rtsao
wants to merge
6
commits into
master
Choose a base branch
from
beta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5891479
Bump react router
rtsao a1d1763
Fix Redirect component
rtsao 1ca9dbd
Release v1.5.3-alpha.0 (#238)
rtsao b75745d
Upgrade to react-router-dom v5
rtsao 1c114c6
Merge branch 'master' into beta
rtsao 29fe8f4
Merge branch 'beta' of github.com:fusionjs/fusion-plugin-react-router…
rtsao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
// $FlowFixMe | ||
import {__RouterContext as RouterContext} from 'react-router-dom'; | ||
|
||
import type {LocationShapeType, RedirectType} from '../types.js'; | ||
|
||
type PropsType = {| | ||
|
@@ -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, | ||
}> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
|
||
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) { | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.