什么是“单一的真相来源”?意思? [英] What does the "single source of truth" mean?

查看:283
本文介绍了什么是“单一的真相来源”?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读



-





这也许有用:< a href =https://redux.js.org/docs/basics/DataFlow.html\"rel =noreferrer> https://redux.js.org/docs/basics/DataFlow.html



在React-Redux应用程序中,当您的Redux是单一事实来源时,这意味着在UI中更改数据的唯一方法是调度redux动作,这将改变redux reducer中的状态。而你的React组件会观察这个减速器,如果减速器发生变化,那么UI也会自行改变。但从来没有其他方式,因为Redux状态是单一的事实来源。



这是它在Redux世界中的样子:





一个实际的例子就是你有Redux商店,其中包含你想要展示的商品。要更改要显示的项目列表,不要在store以外的任何其他位置更改此数据。如果改变了,那么与之相关的其他一切也应该改变。


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?

解决方案

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.

这篇关于什么是“单一的真相来源”?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆