简体   繁体   English

为什么 React 中的接口很有用?

[英]Why is interface in React useful?

I was learning Typescript's interface and its use in React.我正在学习 Typescript 的界面及其在 React 中的使用。 Moreover, as I found out(I hope I am correct:)), interface( used, for example, for prop) is needed to ensure that we can pass only certain props defined in interface prop.此外,正如我发现的(我希望我是正确的:)),需要接口(例如,用于道具)以确保我们只能传递接口道具中定义的某些道具。 The question is what is the benefit of the use of interfaces to serve as kinda "Protection" to be not able to pass any props.问题是使用接口作为“保护”不能传递任何道具的好处是什么。

It is a way to ensure that your components always get the right prop types.这是一种确保您的组件始终获得正确的 prop 类型的方法。

It can also serve as documentation for other developers so they will know what props to pass to your components.它还可以作为其他开发人员的文档,以便他们知道将哪些 props 传递给您的组件。

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

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