Quick example: pass data to a React component and get the changed value back
An quick example how to pass data to componenents and get data from components
⚠ Disclaimer
This is a quick example how I pass data to a React component and get the changed data back. If there are better solutions, please let me know.
Overview
In different cases it’s neccessary to pass data to a component and get the data back. In this quick example there are two child components. One is used to determine the height of increase in the count. The other one is used to increase the count via a button click with the height of increase from the other component.
The App.js contains the two child components CounterSteps.js and Button.js.
From the CounterSteps.js the App.js get the height of the increase via a event and store it in the counterSteps state. The counterSteps value will be passed to Button.js. After each time the Button was pressed the App.js get the value back.
CounterSteps.js
In the CounterSteps.js is a input field. Every change of the value will be passed via a event to the parent component.
Button.js
The Button.js receive the height of the inccrease from the parent component. A Button click calls the event and pass the height of increase back. In the App.js the total count is calculated.