Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Pass route params to handlers and demonstrate how they are used #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import routes from "views/Routes";
/**
* Fire-up React Router.
*/
Router.run(routes, Router.HistoryLocation, (Handler) => {
Transmit.render(Handler, {}, document.getElementById("react-root"));
Router.run(routes, Router.HistoryLocation, (Handler, router) => {
Transmit.render(Handler, {queryParams: router.params}, document.getElementById("react-root"));
});

/**
Expand Down
21 changes: 20 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ server.route({
}
});

server.route({
method: "GET",
path: "/api/names/{nameId}",
handler: (request, reply) => {
const exampleNames = {
"1": "John",
"2": "Jack",
"3": "Jane"
};

const name = exampleNames[request.params.nameId];
const result = {
name: name ? name : "Name not found"
};

reply(result);
}
});

/**
* Catch dynamic requests here to fire-up React Router.
*/
Expand All @@ -58,7 +77,7 @@ server.ext("onPreResponse", (request, reply) => {
}

Router.run(routes, request.path, (Handler, router) => {
Transmit.renderToString(Handler).then(({reactString, reactData}) => {
Transmit.renderToString(Handler, {queryParams: router.params}).then(({reactString, reactData}) => {
let output = (
`<!doctype html>
<html lang="en-us">
Expand Down
21 changes: 21 additions & 0 deletions src/views/Greeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react";
import InlineCss from "react-inline-css";
import Transmit from "react-transmit";

class Greeting extends React.Component {
render () {
return <h1>Hello, {this.props.person.name}</h1>;
}
}

export default Transmit.createContainer(Greeting, {
queries: {
person (queryParams) {
// Calling the API when rendering on the server and when rendering
// on the client.

const api = `http://localhost:8000/api/names/${queryParams.personId}`;
return fetch(api).then((response) => response.json());
}
}
});
2 changes: 2 additions & 0 deletions src/views/Routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import {Route, DefaultRoute} from "react-router";
import Main from "views/Main";
import Greeting from "views/Greeting";

/**
* The React Routes for both the server and the client.
Expand All @@ -10,5 +11,6 @@ import Main from "views/Main";
export default (
<Route path="/">
<DefaultRoute handler={Main} />
<Route name="greeting" handler={Greeting} path="greeting/:personId" />
</Route>
);