Skip to content

Commit 9d68433

Browse files
committed
Fix for onClick handler not resolving "to" location correctly
1 parent 1847da6 commit 9d68433

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/LinkContainer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class LinkContainer extends React.Component {
6262
to, query, hash, state, children, onClick, target, action,
6363
} = this.props;
6464

65+
const { router } = this.context;
66+
67+
const toLocation = resolveToLocation(to, router);
68+
6569
if (children.props.onClick) {
6670
children.props.onClick(event);
6771
}
@@ -81,8 +85,8 @@ class LinkContainer extends React.Component {
8185

8286
event.preventDefault();
8387

84-
this.context.router[action](
85-
createLocationDescriptor(to, query, hash, state)
88+
router[action](
89+
createLocationDescriptor(toLocation, query, hash, state)
8690
);
8791
};
8892

test/LinkContainer.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,39 @@ describe('LinkContainer', () => {
127127
expect(target).to.exist;
128128
});
129129

130+
it('should transition to the correct route, with "to" prop as a function', () => {
131+
const router = ReactTestUtils.renderIntoDocument(
132+
<Router history={createMemoryHistory('/')}>
133+
<Route
134+
path="/"
135+
component={() => (
136+
<LinkContainer
137+
to={() => ({
138+
pathname: '/target',
139+
})}
140+
>
141+
<Component>Target</Component>
142+
</LinkContainer>
143+
)}
144+
/>
145+
<Route
146+
path="/target"
147+
component={() => <div className="target" />}
148+
/>
149+
</Router>
150+
);
151+
152+
const anchor = ReactTestUtils.findRenderedDOMComponentWithTag(
153+
router, 'A'
154+
);
155+
ReactTestUtils.Simulate.click(anchor, { button: 0 });
156+
157+
const target = ReactTestUtils.findRenderedDOMComponentWithClass(
158+
router, 'target'
159+
);
160+
expect(target).to.exist;
161+
});
162+
130163
it('should call user defined click handlers', () => {
131164
const onClick = sinon.spy();
132165
const childOnClick = sinon.spy();

0 commit comments

Comments
 (0)