Creating a component in React.js involves several steps. Here’s how you can do it:
First of all, makes sure your development environment is set up to handle React.js. If it’s not, you might want to use Create React App or a similar setup.
Here’s an example of creating a basic functional component:
1. Import the React library
```
import React from ‘react’;
```
1. Next, define the component. In React, you define components as JavaScript functions. Here’s a basic example of a functional component called `MyComponent`:
```
function MyComponent() {
return (
1. Finally, you’ll need to export the component so it can be used in other parts of your application:
```
export default MyComponent;
```
The complete code for the component looks like this:
```
import React from ‘react’;
function MyComponent() { return (
export default MyComponent;
```
This code displays a simple “Hello, World!” message. It is the smallest building block of a React app.
You can also create class components in React by extending the `React.Component` class.
Here’s an example:
```
import React from ‘react’;
class MyComponent extends React.Component { render() { return (
export default MyComponent;
```
In this example, the `MyComponent` class extends `React.Component`, meaning it inherits functionality from it. The `render` method is used to define what the component’s output should be.