Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get state value from one function to another in ReactJS

Understanding State in ReactJS

Before we dive into how to get state value from one function to another in ReactJS, let's take a moment to understand what "state" is. In ReactJS, "state" is like a data store for components. It's like a small database where a component can store important information it needs to use.

Here's a simple analogy to help you understand: imagine a component is like a person, and state is like their memory. Just as a person uses their memory to remember important things, a component uses its state to remember important data.

Creating State in a Function

Now, let's learn how to create a state in a function. In ReactJS, we use a built-in function called useState to create a new state. When you call useState, you need to give it an initial value, and it gives you back two things: the current state value and a function you can use to update the state.

Here's a simple example:

import React, { useState } from 'react';

function MyComponent() {
  const [myState, setMyState] = useState('Hello, world!');

  return (
    <div>
      {myState}
    </div>
  );
}

In this example, myState is our state value, and setMyState is the function we can use to update the state. The initial value of myState is 'Hello, world!'.

Updating State in a Function

To update the state in a function, you simply call the function that useState gave you. Here's an example:

import React, { useState } from 'react';

function MyComponent() {
  const [myState, setMyState] = useState('Hello, world!');

  function handleClick() {
    setMyState('Goodbye, world!');
  }

  return (
    <div>
      {myState}
      <button onClick={handleClick}>Click me</button>
    </div>
  );
}

In this example, when you click the button, it calls the handleClick function, which in turn calls setMyState with a new value. This updates the state, and the component re-renders with the new state value.

Getting State Value from One Function to Another

Now, here comes the main part of our discussion: how to get state value from one function to another.

Let's say we have two components: ParentComponent and ChildComponent. ParentComponent has a state called parentState, and we want to pass this state to ChildComponent.

Here's how we can do it:

import React, { useState } from 'react';

function ParentComponent() {
  const [parentState, setParentState] = useState('Hello, world!');

  return <ChildComponent parentState={parentState} />;
}

function ChildComponent(props) {
  return <div>{props.parentState}</div>;
}

In this example, we're passing parentState as a prop to ChildComponent. Inside ChildComponent, we can access this prop using props.parentState.

This is like handing over a piece of memory (state) from one person (component) to another. Just like how you might tell a friend a piece of information, and they can then use that information in their own way.

Conclusion

State is an essential concept in ReactJS, acting as the memory for our components. Understanding how to create, update, and pass state from one function to another is crucial for building dynamic and interactive web applications.

Just remember, states are like pieces of memory that components use to store important information. You can create a state with useState, update it with the function that useState gives you, and pass it from one component to another using props.

So, as you continue your journey in learning ReactJS, don't forget to take state along with you, because they are your components' best friend, their memory keeper.