Created
April 18, 2021 23:06
-
-
Save aspirinchaos/d8bb5a84fcebaa27b6f0c111ba81990c to your computer and use it in GitHub Desktop.
Meteor + React server render
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 const replacePattern = (template, data) => { | |
| const _pattern = /\{\{(.*?)\}\}/g; // {{property}} | |
| return template.replace(_pattern, (match, token) => data[token] || ''); | |
| }; |
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
| import React from 'react'; | |
| import PropTypes from 'prop-types'; | |
| import RDS from 'react-dom/server'; | |
| import { replacePattern } from './helpers'; | |
| import MyComponent from './MyComponent'; // a react component stored on server, renders a list of users | |
| const renderOnServer = (_id) => { | |
| const users = Meteor.users.find().fetch(); | |
| const data = { | |
| content: RDS.renderToStaticMarkup(<MyComponent users={users} />), | |
| title: 'MyComponent render', | |
| }; | |
| return replacePattern(template, data); | |
| }; |
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
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>{{title}}</title> | |
| <style> | |
| // css styles | |
| </style> | |
| </head> | |
| <body> | |
| <h1>{{title}}</h1> | |
| {{content}} | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment