From 5a3f2bb019aec5bf23f65f9e61690226f291ea88 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Sun, 23 Dec 2018 22:19:14 +0100 Subject: [PATCH] feat: Remove context usages (#107) React-Redux 6 compatibility by removing legacy context conventions --- src/Link.js | 13 +++++-------- src/NavLink.js | 22 ++++++---------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/Link.js b/src/Link.js index 3516b02..d5edf40 100644 --- a/src/Link.js +++ b/src/Link.js @@ -40,6 +40,7 @@ export const Link = ( { to, href, + location, redirect, replace, tagName = 'a', @@ -51,12 +52,10 @@ export const Link = ( target, dispatch, ...props - }: Props, - { store }: Context + }: Props ) => { to = href || to // href is deprecated and will be removed in next major version - const location = selectLocationState(store.getState()) const { routesMap } = location const url = toUrl(to, routesMap) const handler = handlePress.bind( @@ -98,11 +97,9 @@ export const Link = ( ) } -Link.contextTypes = { - store: PropTypes.object.isRequired -} - -const connector: Connector = connect() +const mapState = state => ({ location: selectLocationState(state) }) +const mapProps = dispatch => ({ dispatch }) +const connector: Connector = connect(mapState, mapProps) // $FlowIgnore export default connector(Link) diff --git a/src/NavLink.js b/src/NavLink.js index 4ff918e..a680dc2 100644 --- a/src/NavLink.js +++ b/src/NavLink.js @@ -36,19 +36,14 @@ type OwnProps = { } type Props = { - dispatch: Function, - pathname: string + dispatch: Function } & OwnProps -type Context = { - store: Store<*, *> -} - const NavLink = ( { to, href, - pathname, + location, className, style, activeClassName = 'active', @@ -58,18 +53,16 @@ const NavLink = ( strict, isActive, ...props - }: Props, - { store }: Context + }: Props ) => { to = href || to const options = getOptions() const basename = options.basename ? options.basename : '' - const location = selectLocationState(store.getState()) const path = toUrl(to, location.routesMap).split('?')[0] - const match = matchPath(pathname, { + const match = matchPath(location.pathname, { path: stripBasename(path, basename), exact, strict @@ -89,16 +82,13 @@ const NavLink = ( className={combinedClassName} style={combinedStyle} aria-current={active && ariaCurrent} + location={location} {...props} /> ) } -NavLink.contextTypes = { - store: PropTypes.object.isRequired -} - -const mapState = state => ({ pathname: selectLocationState(state).pathname }) +const mapState = state => ({ location: selectLocationState(state) }) const connector: Connector = connect(mapState) // $FlowIgnore