-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 4b: use react-transmit to declaratively define data deps
There are multiple ways to solve async rendering issue. Here we'll use react-transmit to declaratively define our data dependencies per component, and return rendered html only when all data is resolved. It works even with nested components, constructing the single promises tree, which is qute cool. It is inspired by Facebook Relay, so if you are familiar with it, you'll feel right at home.
- Loading branch information
Showing
7 changed files
with
43 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import React from 'react'; | ||
import s from 'Hello.scss'; | ||
import Transmit from 'react-transmit'; | ||
import fetch from 'isomorphic-fetch'; | ||
import s from 'Hello.scss'; | ||
|
||
const Hello = React.createClass({ | ||
getInitialState: function() { | ||
return { | ||
hello: '' | ||
}; | ||
}, | ||
componentDidMount: function() { | ||
fetch('/static/data.json') | ||
.then(r => r.json()) | ||
.then(r => this.setState({hello: r.hello})); | ||
}, | ||
render: function() { | ||
return <div className={s.root}>Hello {this.props.name}. Async hello {this.state.hello}</div>; | ||
return <div className={s.root}>Hello {this.props.name}. Async hello {this.props.hello}</div>; | ||
} | ||
}); | ||
|
||
export default Hello; | ||
export default Transmit.createContainer(Hello, { | ||
// These must be set, or else it would fail to render | ||
initialVariables: {}, | ||
// each fragmen will be resolved into a prop | ||
fragments: { | ||
hello () { | ||
return fetch('http://localhost:3000/static/data.json') | ||
.then(r => r.json()) | ||
.then(r => r.hello); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Transmit from 'react-transmit'; | ||
import Hello from 'Hello'; | ||
|
||
ReactDOM.render( | ||
<Hello name="World" />, | ||
Transmit.render( | ||
Hello, | ||
{name: 'World'}, | ||
document.getElementById('app') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,5 @@ | |
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="/built/client.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import fs from 'fs'; | ||
import React from 'react'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import Transmit from 'react-transmit'; | ||
import Hello from './Hello.js'; | ||
|
||
function handleRender(req, res) { | ||
const html = ReactDOMServer.renderToString( | ||
<Hello name="World" /> | ||
); | ||
fs.readFile('./index.html', 'utf8', function (err, file) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
const document = file.replace(/<div id="app"><\/div>/, `<div id="app">${html}</div>`); | ||
res.send(document); | ||
}); | ||
Transmit.renderToString(Hello, {name: 'World'}) | ||
.then(({reactString, reactData}) => { | ||
fs.readFile('./index.html', 'utf8', function (err, file) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
const document = file.replace(/<div id="app"><\/div>/, `<div id="app">${reactString}</div>`); | ||
const output = Transmit.injectIntoMarkup(document, reactData, ['/built/client.js']); | ||
res.send(output); | ||
}); | ||
|
||
}) | ||
.catch(e => console.log(e)); | ||
} | ||
|
||
export default handleRender; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"hello": "world!" | ||
"hello": "WORLD!" | ||
} |