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.tsx205 chars14 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.tsx170 chars6 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