Code explanation
In the above App.js file:
- Line 5: The - Appcomponent (the parent) holds the state variable- message. This state represents data that is passed to the child component.
 
- Lines 7–9: The - updateMessagefunction changes the- messagestate when the button (line 14) is clicked. Since React’s state is owned by the parent, it controls how and when the state is updated.
 
- Line 16: The - messagestate is passed as a prop to the- ChildComponent. Props are read-only in the child, meaning the child cannot modify the- messagedirectly.
 
When the state is updated, React rerenders the App and automatically passes the updated message to ChildComponent. The child displays the new data, maintaining React’s unidirectional data flow.
Effect of state changes in React components
In React, each state is owned by a single component. Due to the unidirectional data flow, changes made to a component’s state will only affect its children, not its parent or sibling components. In the example above, since the state is owned by the App component, changes to the message state only affect App and ChildComponent, keeping the component hierarchy organized and interactions clear. This unidirectional data flow architecture ensures predictable state management and minimizes unnecessary data changes.