Ivan Kaminskyi

Apr 21, 20235 min

Mastering Custom Component Development in React

React.js is a powerful and popular JavaScript library that is used to build user interfaces. It provides a simple and flexible way to build complex web applications by dividing them into smaller, reusable components. In this article, we will discuss how to build a custom React.js component and how to use it in your web application.

Creating a New React Component

The first step in building a custom React component is to create a new component. To do this, we will create a new file in our project and name it after our component. For example, if we want to create a component that displays a user's name, we can name the file UserName.js. The file should be placed in the same directory as the other components in your project.

In the new file, we will start by importing the React library and the necessary components that we need to build our new component. The code should look like this:

import React from 'react';

const UserName = ({name}) => {
  return (
    <div>
      <h1>Hello, {name}!</h1>
    </div>
  );
};

export default UserName;

In the code above, we have created a new component called UserName. This component is a functional component that accepts a prop called "name". The component returns the JSX code that will be displayed on the screen, including the user's name.


Adding Props to the Component

Now that we have created the basic structure for our component, we can add props to it. Props are used to pass data from one component to another. In our case, we will use props to pass the user's name to the UserName component.

To add props to our component, we need to modify the function to accept a prop called "name". We can do this by adding the prop to the function's arguments:

import React from 'react';

const UserName = ({name}) => {
  return (
    <div>
      <h1>Hello, {name}!</h1>
    </div>
  );
};

export default UserName;

In the code above, we have modified our functional component to accept a prop called "name". We then use this prop to display the user's name in the JSX code.


Using the Component in Your App

Now that we have created our component, we can use it in our app. To use the component, we need to import it into our app and then include it in our JSX code.

import React from 'react';
import UserName from './UserName';

const App = () => {
  return (
    <div>
      <UserName name="John" />
    </div>
  );
};

export default App;

In the code above, we have imported our UserName component and included it in the JSX code for our app. We have also passed a prop called "name" with the value "John". When our app is rendered, the UserName component will display the text "Hello, John!" on the screen.


Styling the Component

Now that we have created and used our component, we can add styles to it to make it look better. React provides several ways to add styles to a component, but the most common way is to use CSS. We can create a separate CSS file for our component and then import it into our component file.

import React from 'react';
import './UserName.css';

const UserName = ({name}) => {
  return (
    <div className="user-name">
      <h1>Hello, {name}!</h1>
    </div>
  );
};

export default UserName;

In the code above, we have added a className of "user-name" to our component's outer div. We have also imported a CSS file called "UserName.css" that contains the styling for our component. Here's an example of what our CSS file could look like:

.user-name {
  background-color: #f5f5f5;
  border: 1px solid #ccc;
  padding: 20px;
  text-align: center;
  font-size: 24px;
}

In the CSS code above, we have added some basic styling to our component. The background color is set to a light grey, with a border around the component. We have also added some padding to give the component some breathing room, and centered the text using the text-align property. Finally, we have increased the font size to 24 pixels.

Once we have added the styles to our CSS file, we can import it into our component file using the following code:

import React from 'react';
import './UserName.css';

const UserName = ({name}) => {
  return (
    <div className="user-name">
      <h1>Hello, {name}!</h1>
    </div>
  );
};

export default UserName;

In the code above, we have imported our CSS file by including it in the import statement. We have then added the className "user-name" to our component's outer div. When our app is rendered, the component will display with the styles that we have defined in our CSS file.


Conclusion

In this article, we have discussed how to create a custom React component and how to use it in your web application. We started by creating a new component file and importing the necessary components. We then added props to our component and used it in our app by importing it and passing in the necessary props. Finally, we added some basic styling to our component using CSS.

By following these steps, you can create custom React components for your web application that are reusable, easy to maintain, and look great. With React, you can build complex web applications quickly and efficiently, while maintaining a high level of code quality and maintainability.

Tags:
React
Share:

Related Posts

Get The Latest Insights straight To Your Inbox For Free!

Subscribe To The NewsletterSubscribe To The NewsletterSubscribe To The Newsletter