将诸如Onchange和OnClick之类的参数传递给与TypeScript的React组件
我正在尝试使用React创建一个页面,因为我正在使用Typescript。
在项目中,有一个组件是一个输入错误,如何正确传递onChange
?我知道如何使用JavaScript进行操作,但不使用Typescript进行操作。
我将代码如下:
import { StyledInput } from "./Input.styles";
export const Input = ( { onChange } ) => {
return(
<StyledInput onChange={onChange}/>
)
}
I'm trying to create a page with react, for that I'm using typescript.
In the project there is a component that is an input and in it I want there to be an onChange
function, but when I put it in the component, it has a red dash, indicating that there is an error, how could I pass the onChange
correctly? I know how to do it using javascript, but not with typescript.
I made the code as follows:
import { StyledInput } from "./Input.styles";
export const Input = ( { onChange } ) => {
return(
<StyledInput onChange={onChange}/>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要定义道具类型,例如
或更准确地说,
此方法可确保如果您是第三方包裹类型的签名更改,则仍然具有在道具中定义的相同精确类型
You need to define the prop type, e.g.
Or more precisely
This approach ensures that if you're 3rd party package type signature changes you still have the same precise type defined in your props