Created
June 14, 2017 10:18
-
-
Save madetech-com/fce8078dd054aaa2163861b0ea29b2d7 to your computer and use it in GitHub Desktop.
React & MVC Blog Post
This file contains hidden or 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
// Defining the component | |
const ProfilePage = ({ username, numPosts, numComments }) => | |
<main> | |
<h1>{username}</h1> | |
<span>{numPosts} Posts</span> | |
<span>{numComments} Comments</span> | |
</main> | |
ProfilePage.propTypes = { | |
username: PropTypes.string.isRequired, | |
numPosts: PropTypes.number.isRequired, | |
numComments: PropTypes.number.isRequired | |
} | |
// Rendering the component as HTML | |
ReactDOM.renderToStaticMarkup( | |
<ProfilePage | |
username="test" | |
numPosts={100} | |
numComments={400} | |
/> | |
) |
This file contains hidden or 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
const HomePage = ({ currentUser }) => | |
<main> | |
<header> | |
<Navigation> | |
<NavigationLink to="/profile"> | |
{currentUser.username} | |
</NavigationLink> | |
</Navigation> | |
</header> | |
<article> | |
<h1>Hello World</h1> | |
</article> | |
</main> | |
HomePage.propTypes = { | |
currentUser: PropTypes.shape({ | |
username: PropTypes.string.isRequired | |
}) | |
} | |
HomePage.getInitialProps = async function () { | |
const { username } = await get('/user') | |
return { currentUser: { username } } | |
} |
This file contains hidden or 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
app.get('/basket', async function (req, res) { | |
const user = await Session.getCurrentUser() | |
const lineItems = await Basket.forUser(user) | |
const recommendedItems = await Recommendations.forUser(user) | |
res.renderComponent('BasketPage', { | |
username: user.username, | |
lineItems: lineItems.map(item => ({ | |
name: item.name, | |
quantity: item.quantity, | |
price: item.price | |
})), | |
recommendedItems: lineItems.map(item => ({ | |
name: item.name, | |
image: item.image | |
})) | |
}) | |
}) |
This file contains hidden or 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
function componentMiddleware () { | |
return function (req, res, next) { | |
res.renderComponent = function (pageName, pageProps) { | |
if (this.req.accepts('application/json')) { | |
renderJson({ pageName, pageProps }) | |
} else { | |
renderHtml({ pageName, pageProps }) | |
} | |
} | |
next() | |
} | |
} |
This file contains hidden or 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
{ | |
"page": "Basket", | |
"props": { | |
"username": "trustno1", | |
"lineItems": [ | |
{ "name": "Pencil", "quantity": 100, "price": "£10.00" } | |
], | |
"recommendedItems": [ | |
{ "name": "Water bed", "image": "https://cdn.example.com/img/waterbed-2131244.jpg" } | |
] | |
} | |
} |
This file contains hidden or 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
export { default as HomePage } from './HomePage' | |
export { default as AboutPage } from './AboutPage' | |
export { default as UserProfilePage } from './UserProfilePage' |
This file contains hidden or 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
window.__pageState = { | |
page: 'HomePage', | |
props: { | |
currentUser: { | |
username: 'fox' | |
} | |
} | |
} |
This file contains hidden or 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
const express = require('express') | |
const bodyParser = require('body-parser') | |
const { renderJson, renderHtml } = require('./lib/renderer') | |
const server = express() | |
server.use(bodyParser.json()) | |
server.post('/render', function (req, res) { | |
const { page, props } = req.body | |
if (req.accepts('application/json')) { | |
renderJson({ page, props }) | |
} else { | |
renderHtml({ page, props }) | |
} | |
}) | |
server.listen(3000, () => `Rendering server listening on http://localhost:3000`) |
This file contains hidden or 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
def show | |
user = User.find(params['id']) | |
props = { | |
user: { | |
username: user.username, | |
age: user.age | |
} | |
} | |
render component: { name: 'UserPage', props: props } | |
end |
This file contains hidden or 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
{ | |
"page": "UserPage", | |
"props": { | |
"user": { | |
"username": "scully", | |
"age": 40 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment