Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a React component (ES6 API) directly using ReactDOM?

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.

like image 441
Dynalon Avatar asked Feb 24 '16 10:02

Dynalon


People also ask

What is ReactDOM render ()?

The ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.

Which component do we have to pass to the ReactDOM render () method?

render() The first argument is the element or component we want to render, and the second argument is the HTML element (the target node) to which we want to append it. Generally, when we create our project with create-react-app , it gives us a div with the id of a root inside index.

Why ReactDOM render is not working?

Use createRoot instead" occurs because the ReactDOM. render method has been deprecated. To solve the error, create a root element and use the ReactDOMClient.

Is ReactDOM render necessary?

The render() function is one of the most useful and important functions of ReactDOM. it returns a reference to the component after rendering a React element into the DOM in the provided container (or returns null for stateless components).


1 Answers

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>
like image 159
WitVault Avatar answered Sep 28 '22 01:09

WitVault