Sign Up

Join DevzConnect — where devs connect, code, and level up together. Got questions? Stuck on a bug? Or just wanna help others crush it? Jump in and be part of a community that gets it

Have an account? Sign In

Have an account? Sign In Now

Sign In

Welcome back to DevzConnect — where devs connect, code, and level up together. Ready to pick up where you left off? Dive back in, ask questions, share wins, or help others crush their goals!

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the question so it can be answered easily.

Please choose the appropriate section so the question can be searched easily.

Please choose suitable Keywords Ex: question, poll.

Browse
Type the description thoroughly and in details.

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

DevzConnect

DevzConnect Logo DevzConnect Logo

DevzConnect Navigation

  • Home
  • About
  • Blog
  • Contact
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About
  • Blog
  • Contact
Home/ Questions/Q 413
Next
In Process

DevzConnect Latest Questions

nicko
  • 0
  • 0
nickoBeginner
Asked: February 20, 20252025-02-20T00:53:22+00:00 2025-02-20T00:53:22+00:00In: ReactJs

How can you share state between two child components in React?

  • 0
  • 0

Best way to manage shared state between multiple child components in React??

interviewquestionsreactjs
1
  • 1 1 Answer
  • 356 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Bryan Williamson
    Bryan Williamson Beginner
    2025-02-22T09:51:25+00:00Added an answer on February 22, 2025 at 9:51 am

    Sharing state between two child components in React is a common scenario, and there are several approaches depending on the complexity of your app. Here are the best ways to manage shared state between multiple child components:


    🏆 1. Lifting State Up (Best for Simple Cases)

    Concept: Move the shared state to the nearest common parent component and pass it down via props.

    Example:

    import React, { useState } from 'react';
    
    const Parent = () => {
    const [sharedData, setSharedData] = useState('');
    
    return (
    <div>
    <ChildA sharedData={sharedData} setSharedData={setSharedData} />
    <ChildB sharedData={sharedData} />
    </div>
    );
    };
    
    const ChildA = ({ sharedData, setSharedData }) => {
    return (
    <input
    type="text"
    value={sharedData}
    onChange={(e) => setSharedData(e.target.value)}
    placeholder="Type something..."
    />
    );
    };
    
    const ChildB = ({ sharedData }) => {
    return <h2>ChildB received: {sharedData}</h2>;
    };
    
    export default Parent;

    📝 How it works:

    • The Parent holds the shared state (sharedData) and updates it via setSharedData.
    • ChildA updates the state.
    • ChildB reads and displays the updated state.

    ✅ Best for:

    • Small apps or simple components.
    • When only a few components need to share state.

    🌐 2. React Context API (For Medium Complexity)

    Concept: Use the Context API to create a global state that can be accessed by any child component without prop-drilling.

    Example:

    import React, { useState, createContext, useContext } from 'react';
    
    // Create Context
    const SharedContext = createContext();
    
    const Parent = () => {
    const [sharedData, setSharedData] = useState('');
    
    return (
    <SharedContext.Provider value={{ sharedData, setSharedData }}>
    <ChildA />
    <ChildB />
    </SharedContext.Provider>
    );
    };
    
    const ChildA = () => {
    const { sharedData, setSharedData } = useContext(SharedContext);
    
    return (
    <input
    type="text"
    value={sharedData}
    onChange={(e) => setSharedData(e.target.value)}
    placeholder="Type here..."
    />
    );
    };
    
    const ChildB = () => {
    const { sharedData } = useContext(SharedContext);
    
    return <h2>ChildB received: {sharedData}</h2>;
    };
    
    export default Parent;

    📝 How it works:

    • SharedContext provides the state to ChildA and ChildB.
    • No prop-drilling is needed.
    • ChildA updates the state, and ChildB consumes it.

    ✅ Best for:

    • Medium to large apps.
    • Avoiding prop-drilling when many nested components need access to shared data.

    ⚡ 3. State Management Libraries (For Complex Apps)

    For larger applications where state sharing becomes complex, consider using state management libraries like:

    • Redux 🛠️ (Popular for complex state logic)
    • Recoil 🌿 (Simpler and more modern)
    • Zustand 🐻 (Minimalistic and lightweight)
    • MobX ⚡ (Reactive state management)

    Example with Zustand:

    npm install zustand
    import create from 'zustand';
    
    // Create Zustand store
    const useStore = create((set) => ({
    sharedData: '',
    setSharedData: (data) => set({ sharedData: data }),
    }));
    
    const ChildA = () => {
    const { sharedData, setSharedData } = useStore();
    
    return (
    <input
    type="text"
    value={sharedData}
    onChange={(e) => setSharedData(e.target.value)}
    placeholder="Type here..."
    />
    );
    };
    
    const ChildB = () => {
    const { sharedData } = useStore();
    
    return <h2>ChildB received: {sharedData}</h2>;
    };
    
    const App = () => (
    <div>
    <ChildA />
    <ChildB />
    </div>
    );
    
    export default App;

    ✅ Best for:

    • Complex apps with deeply nested components.
    • Apps requiring global state management.

    🧠 4. URL Params / LocalStorage (For Persisted State)

    • If state needs to persist across page reloads or be shareable via URL, use:
      • URL query params with React Router.
      • LocalStorage/SessionStorage for browser persistence.

    🚀 Which Method to Choose?

    Use Case Recommended Approach
    Simple shared state between siblings Lifting State Up
    Multiple nested components needing shared state Context API
    Complex global state with advanced features Redux, Zustand, Recoil
    Need for persistence across reloads LocalStorage/SessionStorage
    State needs to be shareable via URL URL Params + React Router

      • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226
  • Answers 144
  • Best Answers 4
  • Users 114
  • Popular
  • Answers
  • nicko

    Understanding Debounce in React: Best Practices for Optimizing API Calls and ...

    • 36 Answers
  • nicko

    How does React Server-Side Rendering (SSR) improve SEO and performance ...

    • 2 Answers
  • nicko

    What is the difference between props and state in react?

    • 2 Answers
  • blackpass biz
    blackpass biz added an answer Hey would you mind sharing which blog platform you're working… February 1, 2026 at 6:33 am
  • divisibility
    divisibility added an answer I am regular visitor, how are you everybody? This post… January 18, 2026 at 4:41 am
  • stashpatrick login
    stashpatrick login added an answer Normally I do not learn post on blogs, however I… January 17, 2026 at 11:15 pm

Related Questions

  • токарный станок чпу по металлу

    • 0 Answers
  • Understanding Debounce in React: Best Practices for Optimizing API Calls and ...

    • 36 Answers
  • How does React Server-Side Rendering (SSR) improve SEO and performance ...

    • 2 Answers
  • How do you test React components?

    • 1 Answer
  • What is the difference between REST and GraphQL?

    • 1 Answer

Top Members

Chloe Stewart

Chloe Stewart

  • 0 Questions
  • 51 Points
Teacher
Bryan Williamson

Bryan Williamson

  • 0 Questions
  • 37 Points
Beginner
Finn Phillips

Finn Phillips

  • 0 Questions
  • 35 Points
Beginner

Trending Tags

accsmarket.net beginner contextapi debounce interviewquestions javascript leetcode mongo mongodb nextjs r9hqxc react reactjs seo ssr theory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges

Footer

© 2025 DevzConnect. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.