Giter Site home page Giter Site logo

react-helmet-example's Introduction

react-helmet-example

This is an example of using react-helmet with React and react-router in a universal (client/server) context.

Usage

Check out this repo (or simply copy the code) and then, from inside the codebase, follow these steps:

  1. Install requirements:
$ npm install
  1. Run webpack to create the client-side script
$ webpack
  1. Start the Express server
$ npm start
  1. Visit http://localhost:8080 and check out the document's <head> node in its source. Then, change pages and watch as react-helmet keeps <head> in sync with the current route.

react-helmet-example's People

Contributors

mattdennewitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-helmet-example's Issues

Terraform README

Flesh out the README with more information about what's going on with client and server.

Update for latest Helmet features

Could you update to exemplify usage of base and script tags. Also, update to latest version of Helmet and make sure it's compatible. And if you could show examples of rendering on the server as React components, in addition to rendering by string that would really help a lot of users.

Update to react-router@2

Initially left a comment here, but feel more appropriate to raise an issue here.

@doctyper It seems that several of us here are having issues. Just
confirming that you are using it for server rendering as well?

If so, could you please update the react-helmet-example to use
react-router@2 (it's currently using [email protected]):
https://github.com/mattdennewitz/react-helmet-example/blob/master/package.json#L12

... and then I'd like to see what is done differently when rendering
on the server: https://github.com/mattdennewitz/react-helmet-example/blob/master/server.js#L30-L48

CC @mattdennewitz

Cannot read property 'title' of undefined

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Post. See https://fb.me/react-warning-keys for more information.
Warning: No route matches path "/static/client.js". Make sure you have somewhere in your routes
TypeError: Cannot read property 'title' of undefined
at Function. (/home/yuva/Downloads/react-helmet-example-master/server.js:45:34)
at Function.dispatchHandler (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:379:22)
at /home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:347:29
at Function.Transition.to (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/Transition.js:72:15)
at /home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:346:22
at Function.Transition.from (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/Transition.js:51:15)
at Function.dispatch (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:343:20)
at Function.refresh (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:394:16)
at Function.run (/home/yuva/Downloads/react-helmet-example-master/node_modules/react-router/lib/createRouter.js:390:16)
at /home/yuva/Downloads/react-helmet-example-master/server.js:24:12

TypeError: Cannot read property 'create' of undefined

I am trying to rewrite this with React-router new version.But I can't fix this error.

import express from 'express';
import mysql from 'mysql';
import webpack from 'webpack';
import path from 'path';
import config from './webpack.config.dev';
import React from 'react';

import Router from 'react-router';
import Helmet from 'react-helmet';
import routes from './src/App';

const app = express();

const compiler = webpack(config);

app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
//
// app.use(require('webpack-hot-middleware')(compiler));

app.set('view engine','ejs');
app.use(express.static('./public'));
const pool = mysql.createPool({
connectionLimit:100,
host:'127.0.0.1',
user:'root',
password:'123456',
database:'naveen'
});

app.get('*',function (req,res) {
let router = Router.create({
location:req.url,
routes:routes
});
router.run(function (Root,state) {
let renderedBody = React.renderToString();
let head = Helmet.rewind();

    let html = `
  <!DOCTYPE html>
  <html>
  <head>
     <meta charset="utf-8"/>
     <title>${head.title}</title>
                ${head.meta}
                ${head.link}
  </head>
  <body>
     <div id="app">${renderedBody}</div>
     <script src="./public/bundle.js"></script>

  </body>
  </html>




`;
    res.write(html);
    res.end();




})

});

app.get('/api',function (req,res) {
pool.getConnection(function (err,connection) {
connection.query('SELECT * FROM users',function (err,rows) {
if(err) throw err;
console.log(rows[0]);
res.send(rows);
connection.release()
})
})
});
const port = 3000;
console.log(Server Initializing ${port});
app.listen(port,function (err) {
if(err) throw err;
console.log(I am Alive at ${port})
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.