Skip to main content

Command Palette

Search for a command to run...

Using hooks and sharing data between components using props in reactjs

Reactjs series from beginner to advanced levels ep7

Published
3 min read
Using hooks and sharing data between components using props in reactjs
P

Hello there! I am a full-stack software developer with a passion for front-end development. With years of experience under my belt, I bring a unique combination of technical skills and creative vision to every project I work on. I am dedicated to delivering visually appealing and intuitive interfaces that provide an exceptional user experience. Whether it's building responsive web applications or crafting complex full-stack applications, I am committed to delivering high-quality, scalable, and maintainable code. I am always seeking new challenges and opportunities to grow and stay on the cutting edge of the latest industry trends and technologies. I am excited to share my insights and expertise with you on this platform. Let's learn and grow together!

Using hooks

In React, hooks are functions that allow you to use state and other React features in functional components, instead of having to use class components. There are several built-in hooks, such as useState and useEffect, that can be used to add functionality to your functional components.

useState is a hook that allows you to add state to your functional component. It takes an initial value as an argument, and returns an array with two items: the current state value and a function to update it.

const [count, setCount] = useState(0);

useEffect is a hook that allows you to run side effects in your functional component, such as fetching data or updating the DOM. It takes two arguments: a callback function to run, and an array of dependencies.

useEffect(() => {
    console.log('Effect ran');
}, [count]);

You can also create your own custom hooks to share logic between different components.

import { useState, useEffect } from 'react';

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  }, [friendID]);

  return isOnline;
}

Hooks allow you to write more reusable and composable components and make it easier to manage state and side effects in your application.

Sharing data between components

Often you’ll need components to share data and always update together. For example:

To make two components named MyButton display the same count and update together, you need to move the state from the individual buttons “upwards” to the closest component containing all of them. That is their parent component

In this example, it is MyApp:

Diagram showing a tree of three components, one parent labeled MyApp and two children labeled MyButton. MyApp contains a count value of zero which is passed down to both of the MyButton components, which also show value zero.

Initially, MyApp’s count state is 0 and is passed down to both children

The same diagram as the previous, with the count of the parent MyApp component highlighted indicating a click with the value incremented to one. The flow to both of the children MyButton components is also highlighted, and the count value in each child is set to one indicating the value was passed down.

On click, MyApp updates its count state to 1 and passes it down to both children

Now when you click either button, the count in MyApp will change, which will change both of the counts in MyButton. Here’s how you can express this in code.

Note: Any information passed down from a parent component to a child component it's called a prop.

MyApp.js

import React, { useState } from "react";
import MyButton from "../mybutton/MyButton";
import "./myapp.css"; // Imported css stylesheet

function MyApp() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }
  return (
    <div className="header">
      <h1>Counters that update separately</h1>
      <MyButton count={count} onClick={handleClick} />
      <MyButton count={count} onClick={handleClick} />
    </div>
  );
}

export default MyApp;

MyButton.js

import React from "react";

function MyButton({ count, onClick }) { // used two props: count, onClick
  return (
    <div className="mybutton">
      {/* Any time the button is clicked the count value increases by 1" */}
      <button
        onClick={onClick}
        className="main-btn"
        style={{ margin: "3rem auto", padding: "0.5rem" }}
      >
        Clicked {count} times
      </button>
      {/* inline styles */}
    </div>
  );
}

export default MyButton;

Conclusion

Hooks and props are two important concepts in React.js that allow developers to share data and state between components. Hooks provide a way to use state and other React features in functional components, whereas props allow components to receive data from their parent component. Together, these features allow for greater flexibility and reusability in building React applications.

Weekly podcast

ReactJS

Part 8 of 14

In this series I will be sharing reactjs related content from beginner to advanced levels.

Up next

Responding to events and updating the screen in reactjs

Reactjs series from beginner to advanced levels ep6

More from this blog

E

Expert software Developer with Proven Track Record in Design and Development of High-Quality, User-Friendly Web Apps

34 posts

As a software developer, I am passionate about technology and have a drive to share my knowledge with others. With a focus on new and upcoming software developers, I aim to spread my expertise and experience to support their growth in the industry. Through my experience and dedication, I am constantly striving to expand my understanding of the latest trends and advancements in software development. Whether through writing articles or conducting presentations, I am dedicated to sharing my knowledge and helping others reach their full potential in the field.