Skip to content

Use Public API for React-Router instead of Private Context #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2019
Merged
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
amd
lib
tmp-bower-repo
.idea
package-lock.json
5 changes: 4 additions & 1 deletion src/IndexLinkContainer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React from 'react';
import { withRouter } from 'react-router-dom';

import LinkContainer from './LinkContainer';

// Don't use a stateless function, to allow users to set a ref.
/* eslint-disable react/prefer-stateless-function */
export default class IndexLinkContainer extends React.Component {
export class IndexLinkContainer extends React.Component {
render() {
return (
<LinkContainer {...this.props} exact />
);
}
}
/* eslint-enable react/prefer-stateless-function */

export default withRouter(IndexLinkContainer);
37 changes: 20 additions & 17 deletions src/LinkContainer.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Route } from 'react-router-dom';
import { Route, withRouter } from 'react-router-dom';

const isModifiedEvent = (event) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

export default class LinkContainer extends Component {
static contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
push: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
createHref: PropTypes.func.isRequired,
}).isRequired,
}).isRequired,
};

export class LinkContainer extends Component {
static propTypes = {
history: PropTypes.shape({
push: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
createHref: PropTypes.func.isRequired,
}).isRequired,
location: PropTypes.object,
match: PropTypes.object,
staticContext: PropTypes.object,
children: PropTypes.element.isRequired,
onClick: PropTypes.func,
replace: PropTypes.bool,
Expand Down Expand Up @@ -58,21 +56,24 @@ export default class LinkContainer extends Component {
) {
event.preventDefault();

const { history } = this.context.router;
const { replace, to } = this.props;
const { replace, to, history } = this.props;

if (replace) {
history.replace(to);
} else {
history.push(to);
}
}
}
};

render() {
const {
history,
location: _location, // eslint-disable-line no-unused-vars
match: _match, // eslint-disable-line no-unused-vars
staticContext: _staticContext, // eslint-disable-line no-unused-vars
children,
replace, // eslint-disable-line no-unused-vars
replace, // eslint-disable-line no-unused-vars
to,
exact,
strict,
Expand All @@ -84,7 +85,7 @@ export default class LinkContainer extends Component {
...props,
} = this.props;

const href = this.context.router.history.createHref(
const href = history.createHref(
typeof to === 'string' ? { pathname: to } : to
);

Expand Down Expand Up @@ -114,3 +115,5 @@ export default class LinkContainer extends Component {
);
}
}

export default withRouter(LinkContainer);
4 changes: 2 additions & 2 deletions test/LinkContainer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ReactBootstrap from 'react-bootstrap';
import { findDOMNode } from 'react-dom';
import { Route, MemoryRouter as Router } from 'react-router-dom';

import LinkContainer from '../src/LinkContainer';
import LinkContainer, { LinkContainer as RawLinkContainer } from '../src/LinkContainer';

describe('LinkContainer', () => {
[
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('LinkContainer', () => {
);

const container = ReactTestUtils.findRenderedComponentWithType(
router, LinkContainer
router, RawLinkContainer
);
const component = ReactTestUtils.findRenderedComponentWithType(
router, Component
Expand Down