There are multiple ways to share data between components. You can use one of the following options:

  • If the component to which you want to pass the data is a child of SearchForm component, then you can pass it as a prop.

  • If you are managing state via redux, you can connect components to the redux store and get the required data from the redux store in your components.

  • You can also use React's Context API to share common data between components that may or may not have a parent-child relationship.

  • If the component that needs the data from SearchForm component, is a parent component of SearchForm component, you can pass a callback function to the SearchForm component as a prop and when data is available in SearchForm component, call the callback function, received as a prop, and pass the data as an argument to that callback function.

Answer from Yousaf on Stack Overflow
🌐
React
react.dev › learn › sharing-state-between-components
Sharing State Between Components – React
This principle is also known as having a “single source of truth”. It doesn’t mean that all state lives in one place—but that for each piece of state, there is a specific component that holds that piece of information. Instead of duplicating shared state between components, lift it up to their common shared parent, and pass it down to the children that need it.
🌐
The Odin Project
theodinproject.com › lessons › node-path-react-new-passing-data-between-components
React - Passing Data Between Components
Using data to create customizable reusable components. In React, data is transferred from parent components to child components via props. This data transfer is unidirectional, meaning it flows in only one direction.
Discussions

Sharing data between React components - javascript
I have a search form in React that performs an API call and saves the data called nameservers in-state using the useState() React hook. I'm trying to determine how I can pass this data to another More on stackoverflow.com
🌐 stackoverflow.com
July 19, 2020
Sharing data between components in React
I'm developing an app using Meteor and React as view engine Consider this diagram: React hide component from another example I need to change C2 component state when C4 button click event is fir... More on stackoverflow.com
🌐 stackoverflow.com
July 29, 2016
The most challenging thing for me about React is sharing state variables between components.
I don’t think people should be recommending state libraries and instead OP needs to understand the pattern of lifting state and managing state with just react. Edit: incorrectly referred to lifting state as hoisting More on reddit.com
🌐 r/reactjs
102
118
June 27, 2023
reactjs - How can I pass data between two React components, but the two components have no parent-child relationship? - Stack Overflow
So how can I pass data in this scenario? Thanks in advance. ... "Context API will not work here" - Why not? If the components have no known-ahead-of-time parent/child relationship then there's one universal parent/child relationship that all components share... They're all children of . Maybe the context just needs to be defined at the top level and both of the components access it? ... Mr. @David , Sorry. I have very poor knowledge in react ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is React Context?
React Context is a built-in feature that allows you to pass data through a component tree without having to pass props down manually at every level.
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Use React Context to Share Data between Components | ...
When should I use React Context?
React Context is ideal for sharing global data like themes, user information, or settings across multiple components without prop drilling.
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Use React Context to Share Data between Components | ...
Can React Context replace state management libraries like Redux?
React Context can replace state management libraries for simple scenarios, but for complex state management, libraries like Redux might still be more appropriate.
🌐
pluralsight.com
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Use React Context to Share Data between Components | ...
🌐
Medium
medium.com › @ruthmpardee › passing-data-between-react-components-103ad82ebd17
Passing Data Between React Components | by Ruth M. Pardee | Medium
May 29, 2017 - Here’s what that might look like if I had data in ToDoItem that I need to access in ToDoList: class ToDoList extends React.Component { myCallback = (dataFromChild) => { [...we will use the dataFromChild here...] }, render() { return ( <div> <ToDoItem callbackFromParent={this.myCallback}/> </div> ); } }
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Use React Context to Share Data between Components | Pluralsight
April 24, 2024 - A workaround for that is to have a dedicated Context/Provider for each functional section that share data between its own components. Then you will reduce the number of components rendering as you will only have updates on sub-trees of your app. In this guide, we explored how we can easily use React Context instead of passing down props to share data between components.
🌐
C# Corner
c-sharpcorner.com › article › how-to-share-data-between-components-in-react
How To Share Data Between Components In React
October 26, 2021 - import React from "react"; import Child from "./child-component"; const Parent = () => { const dataList = [{ name: "Robert", age: 21, role: "Test", }, { name: "Sat", age: 21, role: "Test", }, { name: "mani", age: 20, role: "Software", }, ]; return ( <div> <div class="row m-4 "> <h3>Parent To Child</h3> <Child dataList={dataList} /> </div> </div> ); }; export default Parent;
Top answer
1 of 3
11

There are multiple ways to share data between components. You can use one of the following options:

  • If the component to which you want to pass the data is a child of SearchForm component, then you can pass it as a prop.

  • If you are managing state via redux, you can connect components to the redux store and get the required data from the redux store in your components.

  • You can also use React's Context API to share common data between components that may or may not have a parent-child relationship.

  • If the component that needs the data from SearchForm component, is a parent component of SearchForm component, you can pass a callback function to the SearchForm component as a prop and when data is available in SearchForm component, call the callback function, received as a prop, and pass the data as an argument to that callback function.

2 of 3
3

Ciao, when I want to share data between components I use React-Redux. Lets make an example: Suppose that you want to share data received by server (nameservers). At first install react-redux:

npm install react-redux
npm install redux
npm install @reduxjs/toolkit

Now we have to create the reducer and the action: Lets say you have your component in a folder called "/components/MyComponent". Create a file called MyReducer.js.

/components/MyComponent/MyReducer.js

import { createReducer } from '@reduxjs/toolkit';

const initialState = {
  nameservers: undefined,
};

const LoginReducer = createReducer(initialState, {
   ["SET_NAMESERVERS"]: (state, action) => {
       state.nameservers= action.payload.nameservers;
   },
})

export default MyReducer;

Now, on the same folder, createa file called MyAction.js

/components/MyComponent/MyAction.js

export const setNameServers = data => ({
   type: "SET_NAMESERVERS",
   payload: { nameservers: data }
});

Then create the store: On your project root create a folder callled redux. Inside this create a folder called store. Then on this folder create a file called index.js.

redux/store/index.js

import { createStore, combineReducers } from "redux";
import MyReducer from '../../components/MyComponent/MyReducer';

const reducers = combineReducers({
  MyReducer,
});

const store = createStore(reducers);

export default store;

Now on index.js file on root folder lets pass the store already created:

index.js

...
import { Provider } from 'react-redux';
import store from "./redux/store";

ReactDOM.render((
 <Provider store={store}>
    <App />
 </Provider>
), document.getElementById('root') || document.createElement('div'));

We have almost done. On your component (MyComponent) you retrieve data from server. Once you have data, lets dispatch data to share into the store:

/components/MyComponent/MyComponent.js

...
import { useDispatch } from 'react-redux';
import { setNameServers } from './MyComponentAction';

const MyComponent: React.FC = () => {
   const [nameservers, setNameservers] = useState([]);
   const dispatch = useDispatch();
   ....
   const handleSubmit = (event: any) => {
     ...

fetch(`https://dns.google.com/resolve?name=${domain}&type=NS`)
.then(results => results.json())
.then(data => { 
  setLoading(false);
  if (data && data.Answer) {
    data.Answer.sort((a: any, b: any) => a.data.localeCompare(b.data));
    setNameservers(data.Answer);
    dispatch(setNameServers(data.Answer)); // here the magic
  } 
  });
 };
   
};

Done! now you have nameservers on your react redux store and you can easly get it from another component like this:

OtherComponent.js

import { useSelector } from 'react-redux';
const OtherComponent: React.FC = () => {
   const nameservers = useSelector(state => state.MyReducer.nameservers);
};

And if you log nameservers somewhere in OtherComponent you will see data retrieved in MyComponent. Awesome!

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-pass-data-from-one-component-to-other-component-in-reactjs
How to Pass Data from One Component to Another Component in ReactJS? - GeeksforGeeks
The most common way to pass data between components is through props (short for properties). Props are read-only and allow you to pass information from a parent component to its child components.
Published   July 23, 2025
🌐
Scaler
scaler.com › home › topics › react › passing data between components
Passing Data between Components - React
May 4, 2023 - Redux provides a very useful way to pass data between any components by creating a store. React also provides us with contextAPI, through which we can create context, then provide and consume data through it.
🌐
DEV Community
dev.to › kenbaz › sharing-data-between-components-in-react-9hn
Sharing Data Between Components In React - DEV Community
June 17, 2024 - A parent component can share information with its child component by providing them with props; this data exchange between parent and child helps make components dynamic and reusable.
Top answer
1 of 2
5

You can use any publish/subscribe events library and then make your components listen to any event you need.

For example:

import React from 'react'
import 'events' from 'eventPublishSubscribeLibrary'

class Component2 extends React.Component {
  constructor (props) {
    super(props)
    this.toggleVisibility = this.toogleVisibility.bind(this)
    this.state = {
      visible = true
    }
  }
  componentDidMount () {
    events.subscribe('clicked-button', this.toogleVisibility)
  }
  toogleVisibility () {
    this.setState({
      visible: !this.state.visible
    })
  }
  render () {
    return visible && (
      <div>content</div>
    )
  }
}

const Component4 = () => (
  <button onClick={events.publish('clicked-button')}>Toggle Visibility</button>
)

You can find in this post by davidwalsh a simple implementation for a Pub/Sub JavaScript Object. Or you can search in npm for some other library.

the "right" way

This is the most simple implementation I can think of and for small projects it is a quick an easy solution that should work.

Anyway, as far as the project will grow a bit you will start to have a lot of actions/reactions between your components. With every new component you'll add it'll get more complicated to track all of these relations between all your components. Here is where it comes handy to have the global state of your application stored in one single place, and that is one of the three principles that redux is based on: the single source of truth.

2 of 2
1

I think it's perfect time for you to introduce some state to your app. Try Redux, it's awesome.

🌐
Medium
medium.com › coding-in-depth › reactjs-share-data-between-the-components-de492b129086
ReactJS: Share data between the components | by Coding In depth | Coding In Depth | Medium
May 30, 2020 - From the child component add the product name, code, and description and display value in JSON form in the parent component. Demo screen looks like the below screenshot. ... Writing about Angular, React, JavaScript, Java, C#, NodeJS, AWS, MongoDB, and Redis related articles.
🌐
Codedamn
codedamn.com › news › react js
How can I share data between components in React.js?
October 18, 2023 - If the child components need to modify the state, pass down callback functions via props. What is the Context API? React’s Context API provides a way to share values (data or functions) between components without having to explicitly pass props through every level of the component tree.
🌐
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 - React context is an interface for sharing information with other components without explicitly passing the data as props. 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 ...
🌐
freeCodeCamp
freecodecamp.org › news › pass-data-between-components-in-react
How to Pass Data and Events Between Components in React
June 8, 2021 - Firstly, let's pass data between a parent component and a child component. . First, you'll need to create two components, one parent and one child. import React from 'react' export default function Parent() { return ( <div> </div> ) } import React from 'react' export default function Child() { return ( <div> </div> ) } Next, you'll import the child component in the parent component and return it.
🌐
Jscrambler
jscrambler.com › blog › sharing-data-react-components-context
Sharing Data Across React Components Using Context | Jscrambler
In this tutorial, you'll learn how to share data across React components using Context API, using the Next.js framework to create our web application.
🌐
Pragimtech
pragimtech.com › blog › reactjs › interaction-between-components-in-react
How to Pass data between Components in React
Now We want to allow People to change the salary details let it be Basic or HRA or Special Allowance , Resulting Updated Total Salary in the Employee Component should get displayed. That means we have to Pass the data from Child to Parent. To allow users to change the salary details, lets create state object in the constructor, add respective properties and initialize them with the data from our props.
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Pass Data between React Components | Pluralsight
May 29, 2020 - This is the simplest and most basic ... React. class Parent extends React.Component {state = { data : "Hello World" } render() { return ( <div> <Child1/> //no data to send <Child2 dataFromParent = {this.state.data} /> </div> ); } } //Sending ...
🌐
DEV Community
dev.to › muhammadawaisshaikh › the-simplest-way-to-share-data-between-two-unrelated-components-in-react-1md0
The simplest way to share data between two unrelated Components in react - DEV Community
May 18, 2020 - Here comes the service that actually holds data on the trigger and passes it to the desired data requested components. you have to make a shared service, using exported class in react,