4

I have a React.Component in app.js which I'm trying to render in Express.

app.js:

var React = require('react')

class App extends React.Component {
  render () {
    return <div>Hello</div>
  }
}

export default App

server.js:

var ReactApp = React.createFactory(require('./public/js/bundle.js'))
console.log(ReactDOMServer.renderToString(ReactApp()))

If I use app.js (pre-browserify) to replace the contents of bundle.js, it works! I get the following React app in string form from Express.

<div data-reactid=".av7twqopa8" data-react-checksum="-111206364"><button data-reactid=".av7twqopa8.0">click me!</button></div>

BUT if I use browserify to generate bundle.js via app.js, it throws an error:

 Error: Invariant Violation: Element type is invalid: expected a string       
 (for built-in components) or a class/function (for composite      
 components) but got: object.

The browserify command I'm using is:

"browserify": {
    "transform": [ [ "babelify", { "presets": [ "es2015", "react" ] } ]    ]
 },
 "scripts": {
 "build": "NODE_ENV=production browserify src/main.js | uglifyjs -cm > public/js/bundle.js"
2
  • 1
    Were you able to solve this? I'm having the same issue and there doesn't seem to be any new information available. Commented Apr 28, 2016 at 3:06
  • No but I would check what your passing into React.createFactory(...). Log it out, it may be the wrong thing to pass in. The error is saying something I passed in was the wrong type. It needs to be a function or string. Commented May 27, 2016 at 23:41

1 Answer 1

2

Using module.exports = instead of export default fixed this issue for me.

Sign up to request clarification or add additional context in comments.

1 Comment

Ty. Why does 'modules.exports' work and 'export default' not? I'm trying to use ES6 so ideally would be using export

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.