Skip to content

Commit

Permalink
Expose new convenience function: navigateTo
Browse files Browse the repository at this point in the history
This is a much nicer interface for kicking off a client transition.

This closes redfin#114.
  • Loading branch information
gigabo committed May 26, 2016
1 parent 0db4444 commit 5a22a20
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
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
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
);
}

0 comments on commit 5a22a20

Please sign in to comment.