简体   繁体   English

在React和React Native中使用模型

[英]Using models in React and React native

Context 上下文

Lately, during the development of a personal app there was a misunderstanding between the backend developer and myself. 最近,在个人应用程序的开发过程中,后端开发人员和我自己之间存在误解。 This misunderstanding led to a small change in the API for which the app was not prepared to handle (a field was removed from an object response data) and consequently led to crashes when the related component was mounted. 这种误解导致API发生了一些小变化,而该API并未为该应用做好准备(从对象响应数据中删除了一个字段),因此导致在安装相关组件时崩溃。

Possible solution 可能的解决方案

After that I realized that just by adding a model which could just provide the basic values that the app needs to work, just a basic class implementation of the request (let's say a book data ie), could have leveraged the impact of the issue. 之后,我意识到仅通过添加一个可以提供应用程序需要工作的基本值的模型,就可以对请求进行基本的类实现(比如说书籍数据),就可以利用问题的影响。

Actual question 实际问题

As I've seen few information about the topic I would like to ask the react / react native community if this approach is anti-pattern in some way or it is actually a valid solution. 正如我所看到的有关该主题的信息很少,我想问一下react / react本地社区,这种方法是否在某种程度上是反模式的,或者它实际上是一种有效的解决方案。

You can just use propTypes or defaultProps to catch and handle this issue from the component level. 您可以只使用propTypesdefaultProps从组件级别捕获并处理此问题。

As far as in general having a client validate the response data from an API, I think the way you want to use it could be an anti-pattern in that a client should not be used to test/assert what that response data ought to be. 就一般而言,让客户端验证来自API的响应数据而言,我认为您要使用它的方式可能是一种反模式,因为不应使用客户端来测试/声明应该响应什么响应数据。 。

In your specific situation, perhaps a better solution is to maintain the test cases for the API . 在您的特定情况下,也许更好的解决方案是维护API的测试用例 This will let you officially communicate to your backend developer what your expectations are, and avoid these problems in the future. 这将使您可以正式与后端开发人员交流您的期望,并在将来避免这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM