How do I create a React component? Complete guide with examples

How do I create a React component? Complete guide with examples

React has redefined the way we look at web development, anchoring its position as one of the most popular libraries for building user interfaces. Born out of the desire to simplify and optimize the creation of complex UIs, React emphasizes modular, reusable components, enabling developers to create scalable and maintainable applications with ease.

Components, the fundamental building blocks in React, encapsulate the logic, structure, and style of parts of your UI. They are the backbone of a React application, fostering a philosophy where UIs are built by composing these independent, reusable units.


Before diving into the world of React components, there are some basics you should be comfortable with:

  • JavaScript (ES6+): Since React leans heavily on ES6 features, having a solid understanding of modern JavaScript is essential.
  • Development Environment:
    • Node.js: The JavaScript runtime needed to run React applications.
    • npm (Node Package Manager): The default package manager for Node.js, vital for installing libraries like React.
    • Create React App: An officially supported toolchain to set up a new React project without build configuration.

To ensure everything’s in place, you can visit the official React documentation for a comprehensive setup guide.

What is a React Component?

In React, a component represents a self-contained module that renders some output. Think of it like a LEGO block. Alone, it might not represent much, but when combined with other blocks, it forms a beautiful structure.

React provides two main ways to define components:

  • Class Components: Traditionally used to create components using ES6 classes. They have a more verbose syntax but offer lifecycle methods and local state management.
  • Functional Components: Introduced with the rise of ES6, these offer a more concise way to write components. With the advent of React Hooks, functional components can now leverage features once reserved for class components.

Beyond their individual makeup, components in React are cherished for their reusability. By encapsulating specific UI logic and styling, developers can reuse these components across projects, making code more DRY (Don’t Repeat Yourself) and applications more maintainable.

Understanding JSX

JSX, or JavaScript XML, is a unique syntax extension for JavaScript, commonly used with React. It allows developers to write HTML-like code within their JavaScript code. JSX is not a requirement for React, but it’s recommended for its readable and concise syntax.

Key points about JSX:

  • Difference from HTML: At a glance, JSX might look like HTML. However, there are differences, such as using className instead of class.
  • Embedding JavaScript Expressions: JSX isn’t just a fancy way to write HTML in JS; it can embed any valid JavaScript expression within {}. For instance, {2 + 2} or {user.firstName}.

Creating Your First React Component

Ready to dive in? Let’s create our first React component!

  1. Setting Up a New React Application:
    • First, ensure you have Node.js, npm, and Create React App installed.
    • Create a new project with: npx create-react-app my-first-component
    • Navigate to your new project: cd my-first-component
  2. Writing a Simple Functional Component:
    import React from 'react';

    function Welcome() {
    return <h1>Hello, codedamn!</h1>;

    export default Welcome;

  3. Rendering the Component:
    In src/App.js, import and use the Welcome component.

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

    function App() {
    return <Welcome />;

    export default App;

Run npm start to see your first component in action!

Props in React

Props (short for properties) in React are a way of passing data from parent to child components. They are read-only and help maintain the unidirectional data flow in a React application.

  • Passing Props: To provide a prop to a component, simply add it as an attribute:
    <Welcome name="codedamn" />
  • Accessing Props:
    • In functional components, props are accessed as function arguments:
      function Welcome(props) {
      return <h1>Hello, {}!</h1>;
    • In class components, they’re accessed via this.props:
      class Welcome extends React.Component {
      render() {
      return <h1>Hello, {}!</h1>;

PropTypes and DefaultProps

Props validation is a good practice to ensure that your component has the correct type of data it needs. React offers PropTypes for this.

  • PropTypes: Allows you to specify the type of data a component should receive.
    import PropTypes from 'prop-types';

    Welcome.propTypes = {
    name: PropTypes.string.isRequired

  • DefaultProps: Set default prop values for components:
    Welcome.defaultProps = {
    name: "Visitor"

This means if no name prop is provided, “Visitor” will be used as a default.

State in React

State allows React components to change their output over time in response to user actions, network responses, and more.

  • Class Components:
    • constructor: Initialize state with a state object:
      constructor(props) {
      this.state = { count: 0 };
    • setState: Update state. Always use this method instead of modifying this.state directly.
      this.setState({ count: this.state.count + 1 });
  • Functional Components:
    • useState Hook: Provides state capabilities to functional components.
      1import { useState } from 'react';
      3function Counter() {
      4 const [count, setCount] = useState(0);
      6 return (
      7 <div>
      8 <p>You clicked {count} times</p>
      9 <button onClick={() => setCount(count + 1)}>
      10 Click me
      11 </button>
      12 </div>
      13 );

Component Lifecycle (for Class Components)

In React, every component undergoes a lifecycle, a series of phases determining the birth, update, and death of the component. These phases are:

  1. Mounting: This phase begins when the component is being created and inserted into the DOM.
  2. Updating: Occurs when a component’s state or props change.
  3. Unmounting: The phase when the component is being removed from the DOM.

To handle side effects or additional logic during these phases, React class components provide lifecycle methods, which are special methods that automatically get called as your component achieves specific milestones.

  • componentDidMount(): Called after the component is mounted (i.e., added to the DOM). It’s the perfect place to initiate AJAX requests or set up subscriptions.
  • componentDidUpdate(prevProps, prevState): Invoked after the component updates, i.e., after state or props change.
  • componentWillUnmount(): This method is called right before the component is removed from the DOM, giving you a chance to perform cleanup.

Effects in Functional Components

With the introduction of Hooks in React 16.8, functional components can mimic the behavior of class lifecycle methods. The primary hook for this is useEffect.

useEffect: Allows you to perform side effects in your function components. It takes two arguments: a function containing your effect and a dependency array.

By carefully choosing what goes into the dependency array, useEffect can mimic the three lifecycle phases:

  • Mounting: useEffect(() => {...}, [])
  • Updating: useEffect(() => {...}, [yourDependency])
  • Cleanup: To perform cleanup actions, return a function from your effect, e.g., useEffect(() => { return () => {...} }).

Handling Events

React’s event system is a wrapper around the browser’s native events, bringing consistency and performance improvements.

  • React vs. DOM events: While they might look similar, React events are synthetic, meaning they’re instances of React’s SyntheticEvent class, ensuring events have consistent properties across different browsers.
  • Binding Event Handlers: In class components, it’s common to bind event handlers in the constructor. In functional components, you often don’t need to bind, thanks to arrow functions.
  • Event Handling: Whether you’re in a class or a functional component, handling events is straightforward. You pass a callback function as a prop to a JSX element, and React takes care of the rest.

Forms and Controlled Components

Controlled components in React refer to components where React is in charge of the form input elements and their values. Instead of reading from the DOM, the input values are stored in the component’s state.

  • Handling Inputs: You can manage form inputs by setting the value of the input to a state variable and updating the state on every onChange event.
  • Submitting Data: Use the form’s onSubmit event to handle form submission, making sure to call event.preventDefault() to prevent the default form submission behavior.

Context and Sharing State

Prop drilling refers to the process of passing data down through multiple layers of components. React Context provides a solution.

  • React Context: Create a context using React.createContext(), provide values using a Context.Provider, and consume them using either a Context.Consumer or the useContext hook.

Advanced Patterns

  • Higher-Order Components (HOCs): A pattern where a function takes a component and returns a new component with additional props or behaviors.
  • Render Props: A technique where you pass a function as a child prop, allowing dynamic rendering logic.
  • Compound Components: Components working together, sharing implicit state.
  • React.forwardRef and useRef: Manage refs and forward them through components, useful for focusing inputs or managing DOM measurements.

Styling React Components

React offers flexibility in styling:

  • Inline Styling: Directly in your JSX by passing a style object.
  • CSS Modules: Local scope CSS classes avoiding global namespace collisions.
  • Styled-components/CSS-in-JS: Libraries allowing CSS styles in JavaScript, giving dynamic capabilities.

Performance Optimizations

React provides tools to prevent unnecessary renders:

  • ShouldComponentUpdate and React.PureComponent: In class components, you can dictate when your component should update.
  • memo: Wraps functional components to prevent re-renders if props don’t change.
  • useMemo and useCallback: Hooks to memorize values or callbacks based on dependencies.

Testing React Components

  • Jest and React Testing Library: Tools that aid in writing unit tests for React components. They help simulate rendering, user events, and state changes.
  • Mocking and Simulating: Jest allows you to mock modules and functions, whereas React Testing Library lets you simulate user interactions.

Best Practices

  • Code Splitting: With React.lazy and Suspense, you can split your bundle, reducing the initial load time.
  • Single Responsibility Principle: Every component should have one job. If it gets too complex, break it down.
  • Prop Types: Validate props with prop-types and set default values with defaultProps.


We’ve traversed from the basics of creating a React component to diving deep into advanced patterns. This journey should equip you with the tools and knowledge to construct powerful React applications. Keep experimenting and building!

Additional Resources

  1. Official React Documentation
  2. React Testing Library
  3. Styled-components

I encourage readers to explore these resources and immerse themselves further into the React ecosystem. Remember, practice makes perfect, and codedamn is the perfect platform to keep honing your skills. Happy coding!

Sharing is caring

Did you like what Rishabh Rao wrote? Thank them for their work by sharing it on social media.


No comments so far