Skip to content

Add params prop to link component #109

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

Closed
Closed
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,18 @@ change the path of your route, you don't have to change your links.
**query** - Object, Query parameters to add to the link. Access query
parameters in your route handler with `this.props.query`.

**[param]** - Any parameters the route defines are passed by name
through the link's properties.
**params** - Object, Route parameters to add to the link. Access route
parameters in your route handler with `this.props.params`.

**[param]** - As an alternative to the params prop, route params can be
passed as separate props where the prop name matches the route param name.

#### Example

Given a route like `<Route name="user" path="/users/:userId"/>`:

```xml
<Link to="user" userId={user.id} params={{foo: bar}}>{user.name}</Link>
<Link to="user" userId={user.id} query={{foo: bar}}>{user.name}</Link>
<!-- becomes one of these depending on your router and if the route is
active -->
<a href="/users/123?foo=bar" class="active">Michael</a>
Expand Down
5 changes: 3 additions & 2 deletions modules/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var RESERVED_PROPS = {
className: true,
activeClassName: true,
query: true,
params: true,
children: true // ReactChildren
};

Expand All @@ -27,7 +28,7 @@ var RESERVED_PROPS = {
* <Route name="showPost" path="/posts/:postId" handler={Post}/>
*
* You could use the following component to link to that route:
*
*
* <Link to="showPost" postId="123"/>
*
* In addition to params, links may pass along query string parameters
Expand Down Expand Up @@ -71,7 +72,7 @@ var Link = React.createClass({
* Returns a hash of URL parameters to use in this <Link>'s path.
*/
getParams: function () {
return Link.getUnreservedProps(this.props);
return this.props.params || Link.getUnreservedProps(this.props);
Copy link
Member

Choose a reason for hiding this comment

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

should probably funnel this through Link.getUnreservedProps also, so you don't blow away reserved props on accident.

Copy link
Author

Choose a reason for hiding this comment

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

Using reversed prop names in the params prop actually works just fine. If you wanted, you could have a route param called 'to'.

},

/**
Expand Down