react js in javascript

To use React in JavaScript, you first need to install React using npm or yarn. Once installed, you can create a React component using the React.createClass method or the class syntax.

Here's an example of a simple React component using the class syntax:

index.tsx
import React, { Component } from 'react';

class MyComponent extends Component {
  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
      </div>
    );
  }
}

export default MyComponent;
205 chars
14 lines

In this example, we import React and Component from the React library. Then, we define a class MyComponent that extends from Component. We override the render method to return some JSX which in turn gets rendered to the DOM.

To use this component, you can import it into another JavaScript file:

index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import MyComponent from './MyComponent';

ReactDOM.render(<MyComponent />, document.getElementById('root'));
170 chars
6 lines

In this example, we import React, ReactDOM and our MyComponent component. We then render the component using the ReactDOM.render method and pass it an instance of MyComponent as the first argument and a DOM node as the second argument. The component gets mounted to the DOM and displays the "Hello, world!" message.

gistlibby LogSnag