Flat Preloader Icon

Sharing State Between Components

Sharing state between components in React can be accomplished in several ways, depending on the complexity of the state and the relationship between the components. Here are some common methods:

1. Lifting State Up

When two components need to share state, the common practice is to lift the shared state up to their closest common ancestor. This ancestor component will hold the state and pass it down as props to the children components that need it.
				
					// ParentComponent.js
import React, { useState } from 'react';
import ChildComponentA from './ChildComponentA';
import ChildComponentB from './ChildComponentB';

const ParentComponent = () => {
  const [sharedState, setSharedState] = useState(0);

  return (
    <div>
      <ChildComponentA sharedState={sharedState} setSharedState={setSharedState} />
      <ChildComponentB sharedState={sharedState} setSharedState={setSharedState} />
    </div>
  );
};

export default ParentComponent;

// ChildComponentA.js
import React from 'react';

const ChildComponentA = ({ sharedState, setSharedState }) => {
  return (
    <div>
      <h1>Child A: {sharedState}</h1>
      <button onClick={() => setSharedState(sharedState + 1)}>Increment</button>
    </div>
  );
};

export default ChildComponentA;

// ChildComponentB.js
import React from 'react';

const ChildComponentB = ({ sharedState }) => {
  return (
    <div>
      <h1>Child B: {sharedState}</h1>
    </div>
  );
};

export default ChildComponentB;

				
			

2. Context API

The Context API is useful for sharing state across many components without having to pass props through every level of the tree.
				
					// SharedStateContext.js
import React, { createContext, useState } from 'react';

export const SharedStateContext = createContext();

export const SharedStateProvider = ({ children }) => {
  const [sharedState, setSharedState] = useState(0);

  return (
    <SharedStateContext.Provider value={{ sharedState, setSharedState }}>
      {children}
    </SharedStateContext.Provider>
  );
};

// ParentComponent.js
import React from 'react';
import { SharedStateProvider } from './SharedStateContext';
import ChildComponentA from './ChildComponentA';
import ChildComponentB from './ChildComponentB';

const ParentComponent = () => {
  return (
    <SharedStateProvider>
      <ChildComponentA />
      <ChildComponentB />
    </SharedStateProvider>
  );
};

export default ParentComponent;

// ChildComponentA.js
import React, { useContext } from 'react';
import { SharedStateContext } from './SharedStateContext';

const ChildComponentA = () => {
  const { sharedState, setSharedState } = useContext(SharedStateContext);

  return (
    <div>
      <h1>Child A: {sharedState}</h1>
      <button onClick={() => setSharedState(sharedState + 1)}>Increment</button>
    </div>
  );
};

export default ChildComponentA;

// ChildComponentB.js
import React, { useContext } from 'react';
import { SharedStateContext } from './SharedStateContext';

const ChildComponentB = () => {
  const { sharedState } = useContext(SharedStateContext);

  return (
    <div>
      <h1>Child B: {sharedState}</h1>
    </div>
  );
};

export default ChildComponentB;

				
			

3. Using a State Management Library (Redux, MobX, etc.)

For more complex state management needs, you might use a state management library like Redux or MobX. These libraries provide a more structured way to manage state across your application.