I have read this article. In the "Controlled Components" part, there is a sentence:
We can combine the two by making the React state be the “single source of truth”.
What does the "single source of truth" mean?
A single source of truth (SSOT) is the practice of aggregating the data from many systems within an organization to a single location. A SSOT is not a system, tool, or strategy, but rather a state of being for a company's data in that it can all be found via a single reference point.
A single source of truth (SSOT) methodology is important because it means data sources are up to date and relevant to business decisions. Other benefits include: No duplicate data entries or version control issues. Access timely data values at the right moment.
The concept of Single Source of Truth is essential for all analytics and measurement as it is necessary to have consensus on what data is used for decision making. A single source of truth is where you determine what the core business metrics that you can measure your activity against are.
Specifically in article you linked it talks about 'controlled' and 'uncontrolled' components.
Basically, when you want to implement 'single source of truth', you want to make your components controllable.
By default input fields are not controllable which means it will render data from DOM, not state.
However, if you make your input listen to state instead (therefore making it controllable) it will not be able to change its value unless you change state.
First effect you will notice is that, once you added value property to it, when you type in, nothing will change. And if you add onChange
method that changes state, it will be fully controllable component that only listens to one source of truth; state, instead of DOM events.
--
This is also related to one way data binding. It means that there is only one place which represents state of application, and your UI listens to it. And listening UI will change only if data at this place is changed, never else.
--
Also this might be useful: https://redux.js.org/docs/basics/DataFlow.html
In React-Redux applications, when your Redux is a single source of truth, it means that the only way to change your data in UI is to dispatch redux action which will change state within redux reducer. And your React components will watch this reducer and if that reducer changes, then UI will change itself too. But never other way around, because Redux state is single source of truth.
This is how it looks in Redux world:
A practical example would be that you have Redux store which contains items you want to display. In order to change list of items to be displayed, you don't change this data anywhere else other than store. And if that is changed, everything else related to it, should change as well.
Usually, in HTML + JS, the state/value of the <input>
is controlled by the browser, not javascript. If you also keep the value of such an input in javascript (for any reason), it means there are at least "two sources of truth" - what the browser thinks the value is, and what your code thinks the value is.
With React "controlled components", the two states/values always match, because React always ensures that the browser's (<input>
's) value is equal to the one you provide from javascript (using value
attribute), and so effectively, there is only one "source of truth" left..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With