If you are referring to component state, then hooks will not help you share it between components. Component state is local to the component. If your state lives in context, then useContext hook would be helpful.

Fundamentally, I think you misunderstood the line "sharing stateful logic between components". Stateful logic is different from state. Stateful logic is stuff that you do that modifies state. For e.g., a component subscribing to a store in componentDidMount() and unsubscribing in componentWillUnmount(). This subscribing/unsubscribing behavior can be implemented in a hook and components which need this behavior can just use the hook.

If you want to share state between components, there are various ways to do so, each with its own merits:

1. Lift State Up

Lift state up to a common ancestor component of the two components.

function Ancestor() {
    const [count, setCount] = useState(999);
    return <>
      <DescendantA count={count} onCountChange={setCount} />
      <DescendantB count={count} onCountChange={setCount} />
    </>;
  }

This state sharing approach is not fundamentally different from the traditional way of using state, hooks just give us a different way to declare component state.

2. Context

If the descendants are too deep down in the component hierarchy and you don't want to pass the state down too many layers, you could use the Context API.

There's a useContext hook which you can leverage on within the child components.

3. External State Management Solution

State management libraries like Redux or Mobx or Zustand. Your state will then live in a store outside of React and components can connect/subscribe to the store to receive updates.

Answer from Yangshun Tay on Stack Overflow
๐ŸŒ
React
react.dev โ€บ learn โ€บ sharing-state-between-components
Sharing State Between Components โ€“ React
To do it, remove state from both of them, move it to their closest common parent, and then pass it down to them via props. This is known as lifting state up, and itโ€™s one of the most common things you will do writing React code.
Top answer
1 of 10
164

If you are referring to component state, then hooks will not help you share it between components. Component state is local to the component. If your state lives in context, then useContext hook would be helpful.

Fundamentally, I think you misunderstood the line "sharing stateful logic between components". Stateful logic is different from state. Stateful logic is stuff that you do that modifies state. For e.g., a component subscribing to a store in componentDidMount() and unsubscribing in componentWillUnmount(). This subscribing/unsubscribing behavior can be implemented in a hook and components which need this behavior can just use the hook.

If you want to share state between components, there are various ways to do so, each with its own merits:

1. Lift State Up

Lift state up to a common ancestor component of the two components.

function Ancestor() {
    const [count, setCount] = useState(999);
    return <>
      <DescendantA count={count} onCountChange={setCount} />
      <DescendantB count={count} onCountChange={setCount} />
    </>;
  }

This state sharing approach is not fundamentally different from the traditional way of using state, hooks just give us a different way to declare component state.

2. Context

If the descendants are too deep down in the component hierarchy and you don't want to pass the state down too many layers, you could use the Context API.

There's a useContext hook which you can leverage on within the child components.

3. External State Management Solution

State management libraries like Redux or Mobx or Zustand. Your state will then live in a store outside of React and components can connect/subscribe to the store to receive updates.

2 of 10
96

It is possible without any external state management library. Just use a simple observable implementation:

function makeObservable(target) {
  let listeners = []; // initial listeners can be passed an an argument aswell
  let value = target;

  function get() {
    return value;
  }

  function set(newValue) {
    if (value === newValue) return;
    value = newValue;
    listeners.forEach((l) => l(value));
  }

  function subscribe(listenerFunc) {
    listeners.push(listenerFunc);
    return () => unsubscribe(listenerFunc); // will be used inside React.useEffect
  }

  function unsubscribe(listenerFunc) {
    listeners = listeners.filter((l) => l !== listenerFunc);
  }

  return {
    get,
    set,
    subscribe,
  };
}

And then create a store and hook it to react by using subscribe in useEffect:

const userStore = makeObservable({ name: "user", count: 0 });

const useUser = () => {
  const [user, setUser] = React.useState(userStore.get());

  React.useEffect(() => {
    return userStore.subscribe(setUser);
  }, []);

  const actions = React.useMemo(() => {
    return {
      setName: (name) => userStore.set({ ...user, name }),
      incrementCount: () => userStore.set({ ...user, count: user.count + 1 }),
      decrementCount: () => userStore.set({ ...user, count: user.count - 1 }),
    }
  }, [user])

  return {
    state: user,
    actions
  }
}

And that should work. No need for React.Context or lifting state up

Discussions

How to access one component's state from another component
Find centralized, trusted content ... you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... How do I access one component's state in another component? Below is my code and I'm trying to access the state of component a in component b. var a = React.createClass({ ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
reactjs - React - how to pass state to another component - Stack Overflow
I'm trying to figure out how to notify another component about a state change. Let's say I have 3 components - App.jsx,Header.jsx,and SidebarPush.jsx and all I'm simply trying to do is toggle a class More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I call setState from another Component in ReactJs - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I have two react components and I'd like to call setState to set a state in the one ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does one useState across components OR modules?
Checkout react context https://reactjs.org/docs/context.html . I'd say you are looking at either using that or a state management tool like redux or recoil. More on reddit.com
๐ŸŒ r/reactjs
27
10
January 31, 2022
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ react-access-state-from-another-component-how-to-share-state
Guide to Accessing State from Another Component in React
August 5, 2024 - In this code, ParentComponent has a state variable sharedState passed to both ChildComponentOne and ChildComponentTwo. Both child components can access and display the shared state data, ensuring consistency across different UI parts. As your React app grows, you'll encounter scenarios where multiple components must react to the same state data.
๐ŸŒ
Amit Merchant
amitmerchant.com โ€บ update-state-of-hook-from-other-components-in-react
Update state of component from other components using React hooks
March 6, 2021 - So, if you want to maintain a state in the component you can do it using useState hook like so. import React, { useState } from 'react'; function Example() { // Declare a new state variable, which we'll call "count" const [count, setCount] = ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-share-state-across-react-components-with-context
How To Share State Across React Components with Context | DigitalOcean
July 22, 2020 - This means that you can share information between a parent component and a deeply nested child component, or store site-wide data in a single place and access them anywhere in the application. You can even update data from nested components by providing update functions along with the data. React context is flexible enough to use as a centralized state management system for your project, or you can scope it to smaller sections of your application.
Find elsewhere
๐ŸŒ
Pluralsight
pluralsight.com โ€บ tech insights & how-to guides โ€บ tech guides & tutorials
How to Send State of Current Component as a Parameter to Another External Method Using React | Pluralsight
September 15, 2020 - Sending state to an external method is similar to passing state to a child component. Passing this.state into the external method's parameter will do the trick. Let's take an example of a login form from the React Bootstrap documentation.
Top answer
1 of 3
63

If you work with functional components you can use hooks like useState. Don't forget to "save" (memoize) the reference of your handler with useCallback, it helps React avoid useless rerenders.

Functional component solution

// myContainer.js
import React, { useState } from 'react'
import MyChild from 'some/path/myChild'

function MyContainer() {
  const [name, setName] = useState('foo')

  return (
    <MyChild name={name} onNameChange={setName} />
  )
}

export default MyContainer

// myChild.js
import React, { useCallback } from 'react'

function MyChild({ name, onNameChange }) {

  const handleInputChange = useCallback(event => {
    onNameChange(event.target.value)
  }, [onNameChange])

  return (
    <div>
      <input type="text" onChange={handleInputChange} value={name} />
      <div>The name is: {name}</div>
    </div>
  )
}

export default MyChild

In a class you can use handler (method) that contains some logic or function call(s). It help to keep your code maintainable.

Class component solution

// myContainer.js
import React, { Component } from 'react'
import MyChild from 'some/path/myChild'

class MyContainer extends Component {
  state = {
    name: 'foo'
  }

  handleNameChange = name => {
    this.setState({ name })
  }

  render() {
    return (
      <MyChild name={this.state.name} onNameChange={this.handleNameChange} />
    )
  }

}

export default MyContainer

// myChild.js
import React, { Component } from 'react'

class MyChild extends Component {

  handleInputChange = event => {
    this.props.onNameChange(event.target.value)
  }

  render() {
    return (
      <div>
        <input type="text" onChange={this.handleInputChange} value={this.props.name} />
        <div>The name is: {this.props.name}</div>
      </div>
    )
  }

}

export default MyChild
2 of 3
17

You can't directly call setState on a parent component from a child component because the updating of a component state is restricted to the current component.

To handle this, simply pass a function from the parent to the child that contains setState. So when you want to update the parent's state, just call that passed function.

A minimal example:

// Parent.jsx

import React, { Component } from 'react';
import Child from './Child';

class Parent extends Component {
  constructor(props) {
    super(props);
    this.setChanged = this.setChanged.bind(this);
    this.state = {
      changed: false
    }
  }

  // Function to set the parent's state
  setChanged() {
    this.setState({ changed: true });
  }

  render() {
    return <Child setChanged={this.setChanged} />
  }
}

// Child.js

import React from 'react';

function Child(props) {
  return (
    // When clicked, parent's setChanged function is called
    <div onClick={() => props.setChanged()} />
  )
}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-send-state-props-to-another-component-in-react-with-onclick
How to Send state/props to Another Component in React with onClick? | GeeksforGeeks
April 12, 2025 - As we can't access the props of that component and pass a required argument we can clone it to make the required element and use it in our application. Props: Props ... Transclusion refers to Passing a react component's content to another component.
๐ŸŒ
Medium
medium.com โ€บ @babux1 โ€บ how-to-pass-state-data-from-one-component-to-another-in-react-js-9b4850887163
How to pass state data from one component to another in React.js | by Babux | Medium
February 21, 2023 - There are several ways to pass state to another component in React, including props, context, and callback functions. Each method has its own benefits and drawbacks, and itโ€™s important to choose the right method depending on your use case.โ€ ...
๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ can you update react state from a different component?
r/reactjs on Reddit: Can you update React state from a different component?
September 27, 2022 -

What do I do if I have state in component A and I have another component B in which I have a input field that is supposed to update the state in component A.

In order not going to post my entire code which is a lot, I came up with this simpler situation that still applies to me.

const parentComponent = () => {
 return (
     <div>
         <childOne />
         <childTwo />
     </div>
    );
}


const childOne = () => {
    const [money, setMoney] = useState(5);

    return (
        <div>
            Money Raised: {money}
        </div>
    );
}
const childTwo = () => { 
    return ( 
        <div> 
            <grandChildComponent />//This component is supposed to update money 
        </div 
    ); 
}

Is it possible to update money from grandChildComponent?

The reason why I want to do this is because I have a input field in grandChildComponent that is supposed to get a value and then I want to update the money state with the value that I got from grandChildComponent

I have no idea if I explained this correctly, I'm sorry..

๐ŸŒ
DEV Community
dev.to โ€บ zeyadetman โ€บ how-to-pass-state-between-components-in-reactjs-2pg1
How to pass state between components in reactjs - DEV Community
July 25, 2018 - In this post, i'll explain how to pass a state between components in Reactjs. We'll build a small 'How many books did you read?' app, in this application, we have two main components one big called 'Library' and another small 'Book', we have 3 books in the library state and each book has its ...
๐ŸŒ
Codecademy Forums
discuss.codecademy.com โ€บ frequently asked questions โ€บ react.js faq
Can a component access the state of another component? - React.js FAQ - Codecademy Forums
January 24, 2019 - Answer React components are unable to access the state of other components, because state is private to each component and thus inaccessible. However, there are ways to pass or change state from other components in specific ways.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ The Ultimate Guide to Managing State Between Components in React
How to Manage State Between Components in React
April 2, 2025 - In this example, we create a state variable called count and initialize it with a value of 0. The useState hook returns a pair consisting of the current state value and a function that can be used to update it.
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 322353-how-to-pass-state-variable-to-another-component-in-react
[React] - How to pass state variable to another component | SheCodes
See what students from United States are saying 250,000+ students recommend See reviews ... Learn how to pass a state variable to another component in React by lifting the state up to a common ancestor component and passing it down as a prop.
๐ŸŒ
React
react.dev โ€บ reference โ€บ react โ€บ useState
useState โ€“ React
If you need that, extract a new component and move the state into it. In Strict Mode, React will call your initializer function twice in order to help you find accidental impurities.