📖 Scenario: You are building a simple React app that shows a counter. The counter value is shared between two components: one to display the count and one to increase it.This is like having a shared scoreboard where one person can see the score and another can update it.
🎯 Goal: Create two React functional components that share the same counter state. One component will show the current count, and the other will have a button to increase the count.
📋 What You'll Learn
Use React functional components
Use the useState hook to hold the counter value
Share the counter state between two components via props
Update the counter from the increment button component
Display the current counter value in the display component
💡 Why This Matters
🌍 Real World
Sharing state between components is common in React apps, like sharing user info or app settings across different parts of the UI.
💼 Career
Understanding how to share and update state between components is a key skill for React developers building interactive web apps.
Progress0 / 4 steps
1
Create the initial counter state
Create a React functional component called CounterApp. Inside it, create a state variable called count with initial value 0 using the useState hook.
React
Hint
Use const [count, setCount] = useState(0); inside the CounterApp function.
2
Create the display component
Inside CounterApp, create a new functional component called DisplayCount that accepts a prop called count and returns a <div> showing the text "Current count: " followed by the count value.
React
Hint
Define DisplayCount inside CounterApp and use {count} inside the returned <div>.
3
Create the increment button component
Inside CounterApp, create a new functional component called IncrementButton that accepts a prop called onIncrement. It should return a <button> with the text "Increase". When clicked, it should call the onIncrement function.
React
Hint
Use onClick={onIncrement} on the button inside IncrementButton.
4
Use the components and share state
Inside CounterApp's return statement, render <DisplayCount /> passing the count state as a prop, and <IncrementButton /> passing a function that increases count by 1 as the onIncrement prop.
React
Hint
Use <DisplayCount count={count} /> and <IncrementButton onIncrement={() => setCount(count + 1)} /> inside the return.
Practice
(1/5)
1. What is the main reason to share state between React components?
easy
A. To keep data consistent and allow multiple components to use the same information
B. To make each component have its own separate data
C. To avoid using props in components
D. To prevent components from rendering
Solution
Step 1: Understand the purpose of shared state
Sharing state means keeping data in one place so multiple components can access and update it.
Step 2: Identify the benefit of shared state
This helps keep the app consistent and easier to manage because all components see the same data.
Final Answer:
To keep data consistent and allow multiple components to use the same information -> Option A
Quick Check:
Sharing state = data consistency [OK]
Hint: Shared state means one source of truth for data [OK]
Common Mistakes:
Thinking each component should have its own separate state always
Confusing props with state sharing
Believing sharing state prevents rendering
2. Which of the following is the correct way to pass state and its updater from a parent to a child component in React?
easy
A. <Child state=setState />
B. <Child state={state} setState={setState} />
C. <Child>{setState}</Child>
D. <Child state>{state}</Child>
Solution
Step 1: Recall how props are passed in JSX
Props are passed as attributes with values, like propName={value}.
Step 2: Identify correct syntax for passing state and updater
The parent passes both state and setState as props: <Child state={state} setState={setState} />.
Final Answer:
<Child state={state} setState={setState} /> -> Option B
Quick Check:
Props syntax = attribute=value [OK]
Hint: Props use attribute={value} syntax [OK]
Common Mistakes:
Using children tags incorrectly to pass props
Passing updater as a child instead of prop
Missing curly braces around JavaScript expressions
3. Given this React code, what will be displayed when the button is clicked?
D. setValue prop is passed incorrectly as value instead of the updater function
Solution
Step 1: Check props passed from Parent
Parent passes setValue={value} instead of setValue={setValue}, so setValue is a string, not a function.
Step 2: Understand effect on Child component
Child tries to call setValue as a function on input change, but it is a string, causing an error.
Final Answer:
setValue prop is passed incorrectly as value instead of the updater function -> Option D
Quick Check:
Updater function must be passed, not state value [OK]
Hint: Pass updater function, not state value, as setState prop [OK]
Common Mistakes:
Passing state value instead of updater function
Ignoring prop names mismatch
Assuming onChange syntax is wrong
5. You want to share a counter state between two sibling components in React. Which approach correctly shares the state so both siblings update together?