2

I am using babel-register to use ES6 classes in node environment and want to load and render React component dynamically using require(file_path) with ReactDomServer, but its showing me following error:

"Invariant Violation: renderToStaticMarkup(): You must pass a valid ReactElement."

// enable es6
require('babel-register')({
    "presets": ["es2015", "react"],
    "extensions": [".jsx", ".js"]
});

// load component
var testComponent = require(testComponentPath);

console.log(testComponent); // { default: [Function: TestComponent] }

var html = ReactDomServer.renderToStaticMarkup(testComponent);

1 Answer 1

7

Change your require statement to var testComponent = require(testComponentPath).default;, assuming the component is exported as default.

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

1 Comment

This is outdated and will no longer work. For it to work, you need to hardcode the path: require("your_file.js").default

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.