Can a component access the state of another component?

Question

In the context of this lesson, can a component access the state of another component?

Answer

React components are unable to access the state of other components, because state is private to each component and thus inaccessible.

However, there are ways to pass or change state from other components in specific ways.

To obtain the state of another component, you can pass a component’s state to its child components as props. Also, you can have components share a common ancestor, and have them access the same state in a similar manner.

To change the state of another component, you can pass callback functions, bound to the component, to its child components. When these functions are called, they can change the state of the parent component.

1 Like