Context
Decomposing components
That’s all good, but for now we have everything inside the same component. The input that holds the functionality for changing the state, and the paragraph that actually uses the state in order to render a property of it.
But then we lose the best thing that react provides. Which is the decomposition of components. That means having your application split to many components. But what happens if these components must communicate in order to exchange data?
In our case for example, we could create a completely new component for the paragraph that the only thing that does is to render the paragraph. Wait a second and think. Would this work?
This answer is no! Because we just decompose the component that renders the paragraph, but still we have a reference inside paragraph’s content to the {this.state}. Remember! The new component now has no state!!
So how are we going to access the state of the <MyComponent>? The parent-component and use it inside the child-component?
Simple, the answer is through props! We will go inside the render function of the <MyComponent /> and after the button we will render the <Greeter /> component! Then because the greeting component lies inside the <MyComponent /> instead, we will create a new property during rendering named name, and we will make this equal to the state of the <MyComponent />.
Then inside the <Greeter /> component (that holds the paragraph) instead of using the state, we will use something that paragraph has access to. The props of it’s component!!
At the end, we will have something similar to the example above. Thus you have succeeded to separate tasks to different components, something that is way more manageable, and at the same time you succeeded, to make two separate components to communicate, by putting the one inside the other one. Then the child component, passes to itself the parent's state through it's props.
In other words, props act like a door to Narnia! This is very common in react and props act like doors for passing data from a component to the other one.