-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Add support for URL objects in Link and Router #1345
Changes from 4 commits
87d8a31
1334cf8
2332339
846f307
a23ddc6
906ab66
32f371d
9a768e3
3df3f9d
bba0934
4b54650
3b7785c
5612fbe
0d01f5c
bd34cc7
58a4bf3
7186145
b18b1db
acf7620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,6 +270,24 @@ Each top-level component receives a `url` property with the following API: | |
|
||
The second `as` parameter for `push` and `replace` is an optional _decoration_ of the URL. Useful if you configured custom routes on the server. | ||
|
||
##### With URL object | ||
The component `<Link>` can also receive an URL object and it will automatically format it to create the URL string. | ||
|
||
```jsx | ||
// pages/index.js | ||
import Link from 'next/link' | ||
export default () => ( | ||
<div>Click <Link href={{ pathname: 'about', query: { name: 'Zeit' }}}<a>here</a></Link> to read more</div> | ||
) | ||
``` | ||
|
||
That will generate the URL string `/about?name=Zeit`, you can use the following parameters: | ||
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. How about linking to the existing doc you mentioned (https://nodejs.org/api/url.html#url_url_strings_and_url_objects) instead? Are all the properties supported? What happens if user passes 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. I can link the Node.js documentation yeah, that's probably better. Yes, all properties work, you can create a URL right now using them manually, I don't think it's a problem, the only bug I watched about that is that if you don't set a protocol and set |
||
|
||
- `pathname` - `String` of the link path excluding the query string. If it doesn't start with `/` is automatically added | ||
- `query` - `Object` with the parsed query string. Optional, if you set `search` this is ignored | ||
- `hash` - `String` with the fragment to be used after a `#` character. Optional, if it doesn't start with `#` is automatically added | ||
- `search` - `String` with the stringified query. Optional, if it doesn't start with `?` is automatically added | ||
|
||
#### Imperatively | ||
|
||
<p><details> | ||
|
@@ -302,6 +320,24 @@ The second `as` parameter for `push` and `replace` is an optional _decoration_ o | |
|
||
_Note: in order to programmatically change the route without triggering navigation and component-fetching, use `props.url.push` and `props.url.replace` within a component_ | ||
|
||
##### With URL object | ||
You can use an URL object the same way you use it in a `<Link>` component to `push` and `replace` an url. | ||
|
||
```jsx | ||
import Router from 'next/router' | ||
|
||
const handler = () => Router.push({ | ||
pathname: 'about', | ||
query: { name: 'Zeit' } | ||
}) | ||
|
||
export default () => ( | ||
<div>Click <span onClick={handler}>here</span> to read more</div> | ||
) | ||
``` | ||
|
||
This uses of the same exact parameters as in the `<Link>` component. | ||
|
||
##### Router Events | ||
|
||
You can also listen to different events happening inside the Router. | ||
|
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.
What happen if
as
is also an object. So, we need to first convert bothas
andhref
into the string version.That's easy and less error prone.
Do this in the
constructor
andcomponentWillReceiveProps
and save it to thethis
context. So, we can use it efficiently from everywhere in the link.