Skip to content
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

Expose new convenience function: navigateTo #224

Merged
merged 3 commits into from
May 26, 2016
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
25 changes: 9 additions & 16 deletions docs/client-transitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ That's where _client transitions_ come in. Navigation between `react-server`
pages can be done without ever hitting the server. It's easy. Pages don't
need to do anything tricky to make this work. It's part of the framework!

## The `<Link>`
## The `<Link>` component

How easy is it?

Expand All @@ -27,22 +27,15 @@ transitions. Middle-clicks, command-clicks, etc, all still behave as
expected, but a normal left-click to navigate becomes a light-weight
transition within the browser.

## The `ClientRequest`
## The `navigateTo()` function

Want to manage your own `<a>` tags and click handlers? Want to navigate based
on some other action?

```javascript
const {
ClientRequest
History,
getCurrentRequestContext,
} = require("react-server");

getCurrentRequestContext().navigate(
new ClientRequest(this.props.path),
History.events.PUSHSTATE
);
const {navigateTo} = require("react-server");

navigateTo("/my-page");
```

This is what the `<Link>` tag does for you on click.
Expand All @@ -61,7 +54,7 @@ key/value pair in an `options` object passed as a second argument to the
```javascript
<Link path={path} bundleData={true}>...</Link>

new ClientRequest(path, {bundleData: true});
navigateTo(path, {bundleData: true});
```

In order to render the new page in the browser, we first need to fetch the
Expand All @@ -83,7 +76,7 @@ the results in the bundle and we don't need to fire actual XHRs!
```javascript
<Link path={path} reuseDom={true}>...</Link>

new ClientRequest(path, {reuseDom: true});
navigateTo(path, {reuseDom: true});
```

This is useful for "single-page app" style navigation, where there may be UI
Expand All @@ -98,7 +91,7 @@ page that are the same as the previous page are _re-used_. This includes
```javascript
<Link path={path} frameback={true}>...</Link>

new ClientRequest(path, {frameback: true});
navigateTo(path, {frameback: true});
```

This one addresses a very specific navigation pattern: A _list_ page that's
Expand All @@ -118,7 +111,7 @@ the list.
```javascript
<Link path={path} frameback={true} reuseFrame={true}>...</Link>

new ClientRequest(path, {frameback: true, reuseFrame: true});
navigateTo(path, {frameback: true, reuseFrame: true});
```

This one only affects the behavior of `frameback`. Ordinarily each new
Expand Down
4 changes: 4 additions & 0 deletions packages/react-server-test-pages/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ module.exports = {
entry: "/navigation/data-bundle-cache",
description: "Data bundle cache",
},
NavigateTo: {
entry: "/navigation/navigateTo",
description: "Navigate using `navigateTo()`",
},
ErrorLogs: {
entry: "/error/logs",
description: "Generate errors in the logs",
Expand Down
17 changes: 17 additions & 0 deletions packages/react-server-test-pages/pages/navigation/navigateTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {navigateTo} from "react-server";

const go = page => navigateTo(
`/navigation/navigateTo?cur=${page}`,
{reuseDom: true}
)

const Nav = ({cur}) => <div onClick={() => go(cur+1)}>
<div>Current page: {cur}</div>
<div>Click for next page</div>
</div>

export default class NavigateToPage {
getElements() {
return <Nav cur={+this.getRequest().getQuery().cur||0} />
}
}
1 change: 1 addition & 0 deletions packages/react-server/core/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
RootElement: require("./components/RootElement"),
Link: require('./components/Link'),
History: require('./components/History'),
navigateTo: require('./util/navigateTo').default,
ClientRequest: require('./ClientRequest'),
FramebackController: require('./FramebackController'),
components: {
Expand Down
19 changes: 7 additions & 12 deletions packages/react-server/core/components/Link.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

var React = require('react'),
ClientRequest = require("../ClientRequest"),
History = require("./History"),
getCurrentRequestContext = require("../context/RequestContext").getCurrentRequestContext;
navigateTo = require("../util/navigateTo").default;

module.exports = React.createClass({
displayName: 'Link',
Expand Down Expand Up @@ -39,15 +37,12 @@ module.exports = React.createClass({
e.preventDefault();
e.stopPropagation();
const {bundleData, frameback, reuseDom, reuseFrame} = this.props;
getCurrentRequestContext().navigate(
new ClientRequest(this.props.path, {
bundleData,
frameback,
reuseDom,
reuseFrame,
}),
History.events.PUSHSTATE
);
navigateTo(this.props.path, {
bundleData,
frameback,
reuseDom,
reuseFrame,
});
} else {
// do normal browser navigate
}
Expand Down
12 changes: 12 additions & 0 deletions packages/react-server/core/util/navigateTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ClientRequest from "../ClientRequest";
import History from "../components/History";
import {getCurrentRequestContext} from "../context/RequestContext";

// This initiates a client transition to `path` with `options`.
// Just a convenience wrapper.
export default function navigateTo(path, options) {
getCurrentRequestContext().navigate(
new ClientRequest(path, options),
History.events.PUSHSTATE
);
}