Giter Club home page Giter Club logo

static-react's Introduction

static-react

Zero-configuration CLI React static renderer

Build Status

Usage

npm i -g static-react
static-react RootComponent.js > index.html

Static-react include babel presets and React โ€“ there is no need to install them separately

Examples

See the examples/ directory

Fetching Data

Use the getInitialProps static method to fetch data or get server-side props for things like CSS-in-JS libraries.

import React from 'react'
import fetch from 'isomorphic-fetch'

export default class extends React.Component {
  static getInitialProps = async () => {
    const data = await fetch('https://api.example.com/data')

    return {
      data
    }
  }

  render () {
    const { data } = this.props

    return (
      <html>
        <h1>Data</h1>
        <pre>
          {JSON.stringify(data, null, 2)}
        </pre>
      </html>
    )
  }
}

CSS-in-JS

Use the getInitialProps to pass side effects from CSS-in-JS libraries as props.

import React from 'react'
import { Box } from 'rebass'

export default class Root extends React.Component {
  static getInitialProps = async (app) => {
    const { ServerStyleSheet } = require('styled-components')
    const { renderToString } = require('react-dom/server')
    const sheet = new ServerStyleSheet()
    renderToString(
      sheet.collectStyles(app)
    )
    const css = sheet.getStyleTags()
    return { css }
  }

  static defaultProps = {
    css: ''
  }

  render () {
    const { css } = this.props

    return (
      <html>
        <head>
          ${css}
        </head>
        <body>
          <Box px={3} py={4}>
            Beep boop
          </Box>
        </body>
      </html>
    )
  }
}

MIT License

static-react's People

Contributors

jxnblk avatar npmcdn-to-unpkg-bot avatar tow8ie 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  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  avatar  avatar

static-react's Issues

npm published version is wrong?

There's a difference from the latest version published to npm versus the latest master version. I'm assuming it's not intentional so I wanted to give you a heads up.

In the meantime I'm installing via GitHub URL to make sure I have the latest.

Cheers.

Compilation fails

First of all, thanks for the plugin! Was looking for this for long! But somehow, I am unable to compile components, it errors as follows:

/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395
          throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
          ^

Error: Couldn't find preset "es2015" relative to directory "/Users/jikkujose/ReactJS/scrap/static-react"
    at /Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:387:20)
    at OptionManager.mergePresets (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:370:10)
    at OptionManager.mergeOptions (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:330:14)
    at OptionManager.addConfig (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:232:10)
    at OptionManager.findConfigs (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:436:16)
    at OptionManager.init (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:484:12)
    at compile (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-register/lib/node.js:112:69)
/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395
          throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
          ^

Error: Couldn't find preset "es2015" relative to directory "/Users/jikkujose/ReactJS/scrap/static-react"
    at /Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:387:20)
    at OptionManager.mergePresets (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:370:10)
    at OptionManager.mergeOptions (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:330:14)
    at OptionManager.addConfig (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:232:10)
    at OptionManager.findConfigs (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:436:16)
    at OptionManager.init (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-core/lib/transformation/file/options/option-manager.js:484:12)
    at compile (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-register/lib/node.js:112:69)
    at loader (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-register/lib/node.js:158:14)
    at loader (/Users/jikkujose/ReactJS/scrap/static-react/example/node_modules/babel-register/lib/node.js:158:14)


npmnpm ERR! ERR! Darwin 15.4.0
 Darwin 15.4.0
npmnpm  ERR!ERR!  argv "/usr/local/Cellar/node/5.11.0/bin/node" "/usr/local/Cellar/node/5.11.0/bin/npm" "run" "start"
argv "/usr/local/Cellar/node/5.11.0/bin/node" "/usr/local/Cellar/node/5.11.0/bin/npm" "run" "start"
npm ERR!npm ERR!  node v5.11.0
node v5.11.0
npm npm ERR!ERR!  npm npm  v3.10.3
 v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! code ELIFECYCLE
npmnpm  ERR!ERR! [email protected] start: `static-react components/Root.js > index.html`
 [email protected] start: `static-react components/Root.js > index.html`
npm ERR!npm ERR! Exit status 1
 Exit status 1
npm npm ERR!ERR! 

npmnpm ERR! ERR! Failed at the [email protected] start script 'static-react components/Root.js > index.html'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
 Failed at the [email protected] start script 'static-react components/Root.js > index.html'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR!npm ERR! If you do, this is most likely a problem with the static-react-demo package,
 If you do, this is most likely a problem with the static-react-demo package,
npmnpm  ERR!ERR! not with npm itself.
 not with npm itself.
npm npm ERR!ERR! Tell the author that this fails on your system:
npm Tell the author that this fails on your system:
npm ERR! ERR!     static-react components/Root.js > index.html
     static-react components/Root.js > index.html
npmnpm  ERR!ERR! You can get information on how to open an issue for this project with:
 You can get information on how to open an issue for this project with:
npm npm ERR!ERR!     npm bugs static-react-demo
     npm bugs static-react-demo
npmnpm  ERR!ERR! Or if that isn't available, you can get their info via:
 Or if that isn't available, you can get their info via:
npmnpm  ERR!ERR!     npm owner ls static-react-demo
     npm owner ls static-react-demo
npmnpm  ERR!ERR! There is likely additional logging output above.
 There is likely additional logging output above.


npm ERR! Please include the following file with any support request:
npm ERR! Please include the following file with any support request:
npm ERR!npm ERR!     /Users/jikkujose/ReactJS/scrap/static-react/example/npm-debug.log
     /Users/jikkujose/ReactJS/scrap/static-react/example/npm-debug.log

Syntax errors

Is this repository active? I am getting syntax errors in my project when precompiling
For one component
image

For another component

image

Multiple pages?

I haven't tried this yet , because

static-react components/Root.js > index.html

(as mentioned here)

makes me feel that this only generates a single HTML page...

Can this project generate multiple pages across multiple directories,
like would be required to upload to gh-pages as a static site?

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.