News
بروشور
![](https://www.datocms-assets.com/91338/1672138990-45aa4393-b079-42e1-80f7-13ec397854c4.jpeg)
Lessons Learned
There should be a single “source of truth” for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor. Instead of trying to sync the state between different components, you should rely on the top-down data flow.
Lifting state involves writing more “boilerplate” code than two-way binding approaches, but as a benefit, it takes less work to find and isolate bugs. Since any state “lives” in some component and that component alone can change it, the surface area for bugs is greatly reduced. Additionally, you can implement any custom logic to reject or transform user input.
If something can be derived from either props or state, it probably shouldn’t be in the state. For example, instead of storing both celsiusValue
and fahrenheitValue
, we store just the last edited temperature
and its scale
. The value of the other input can always be calculated from them in the render()
method. This lets us clear or apply rounding to the other field without losing any precision in the user input.