8

I'm using the ES6 classes API of React (by using TypeScript) and want to render a subtyped class of type React.Component using ReactDOM.render().

The following code works:

class MyComponentProps {
    someMember: string;
}

class MyComponent extends React.Component<MyComponentProps, {}> {
    constructor(initialProps: MyComponentProps) {
        super(initialProps);
    }
}

let rootNode = document.getElementById('root');
ReactDOM.render(
    <MyComponent someMember="abcdef" />,
    rootNode
)

Now, given the explicit typing of the constructor in react.d.ts, I'd assume I can pass an instance of the MyCustomProps object that populates with initial properties (as done in the above code). But how to render the component then directly without JSX/TSX syntax?

The following does NOT work:

ReactDOM.render(new MyComponent(new MyComponentProps()), rootNode);

I know I could just use the JSX syntax as a workaround, but as my MyCustomProps object is pretty large, I don't want to repeat every member of the MyCustomProps object.

2
  • 1
    Shouldn't you use React.createElement(MyComponent, new MyComponentProps()) instead of new MyComponent(new MyComponentProps())? Commented Feb 24, 2016 at 10:54
  • Indeed, that'll work - I could also tell by looking at the compiled .jsx code which maps the JSX syntax to exactly that syntax. If you post you comment as an answer, I'll mark it as accepted. Commented Feb 24, 2016 at 11:42

1 Answer 1

15

You should ues React.createElement. React.createElement takes a tag name or component, a properties object, and variable number of optional child arguments. E.g.

class App extends React.Component {
  render(){
    return (

      <div><h1>Welcome to React</h1></div>
    );
  }
}

Using jsx it can be rendered like this

ReactDOM.render(<App />, document.getElementById('app'));

In another way(without jsx)

ReactDOM.render(React.createElement(App, null), document.getElementById('app'));

A running example -

class App extends React.Component {
  render(){
    return (
      <div><h1>Welcome to React</h1></div>
    );
  }
}

ReactDOM.render(React.createElement(App, null), document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app">
</div>

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

3 Comments

The last line of code in your answer is what I was looking for, thx!
Future Googlers: you should change your code to look like this if you are getting TypeError: this is undefined at react.js:5045.
So I have to use ReactDOM.render(React.createElement(), root) instead of ReactDOM.render(<div></div>, root)? Is there anything can be done that makes TypeScript allow JSX literal in ReactDOM.render()

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.