Skip to content

Commit

Permalink
Replace deprecated props.url in readme (#4952)
Browse files Browse the repository at this point in the history
Following #4950

Fix #4716

I'm not sure to understand this part, though :
> 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

Is there a difference between :
```jsx
export default () => <a onClick={() => Router.push('/about')}>About</a>
```

and
 
```jsx
export default withRouter(
  () => <a onClick={() => props.router.push('/about')}>About</a>
)
```
?
  • Loading branch information
lucleray authored and timneutkens committed Aug 13, 2018
1 parent 1889c80 commit fcf9625
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,7 @@ Client-side routing behaves exactly like the browser:
2. If it defines `getInitialProps`, data is fetched. If an error occurs, `_error.js` is rendered
3. After 1 and 2 complete, `pushState` is performed and the new component is rendered

**Deprecated, use [withRouter](https://github.com/zeit/next.js#using-a-higher-order-component) instead** - Each top-level component receives a `url` property with the following API:

- `pathname` - `String` of the current path excluding the query string
- `query` - `Object` with the parsed query string. Defaults to `{}`
- `asPath` - `String` of the actual path (including the query) shows in the browser
To inject the `pathname`, `query` or `asPath` in your component, you can use [withRouter](#using-a-higher-order-component).

##### With URL object

Expand Down Expand Up @@ -518,8 +514,6 @@ Above `Router` object comes 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.

_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.

Expand Down Expand Up @@ -709,39 +703,44 @@ export default () =>
Most prefetching needs are addressed by `<Link />`, but we also expose an imperative API for advanced usage:

```jsx
import Router from 'next/router'
import { withRouter } from 'next/router'

export default ({ url }) =>
export default withRouter(({ router }) =>
<div>
<a onClick={() => setTimeout(() => url.pushTo('/dynamic'), 100)}>
<a onClick={() => setTimeout(() => router.push('/dynamic'), 100)}>
A route transition will happen after 100ms
</a>
{// but we can prefetch it!
Router.prefetch('/dynamic')}
router.prefetch('/dynamic')}
</div>
)
```

The router instance should be only used inside the client side of your app though. In order to prevent any error regarding this subject, when rendering the Router on the server side, use the imperatively prefetch method in the `componentDidMount()` lifecycle method.

```jsx
import React from 'react'
import Router from 'next/router'
import { withRouter } from 'next/router'

export default class MyLink extends React.Component {
class MyLink extends React.Component {
componentDidMount() {
Router.prefetch('/dynamic')
const { router } = this.props
router.prefetch('/dynamic')
}

render() {
const { router } = this.props
return (
<div>
<a onClick={() => setTimeout(() => url.pushTo('/dynamic'), 100)}>
<a onClick={() => setTimeout(() => router.push('/dynamic'), 100)}>
A route transition will happen after 100ms
</a>
</div>
)
}
}

export default withRouter(MyLink)
```

### Custom server and routing
Expand Down

0 comments on commit fcf9625

Please sign in to comment.