【发布时间】:2021-05-12 15:22:27
【问题描述】:
如何使用展开操作来解构传递给组件的道具?我已经尝试了一些方法来做到这一点,但没有任何运气。
import React from 'react';
import "./button.component.css"
function ButtonComponent( {onClick: ()=> void, caption:string, ...otherProps}) {
return (
<button onClick={onClick} className="button" {...otherProps}>{caption}</button>
);
};
export default ButtonComponent;
这给出了以下错误:
Binding element 'onClick' implicitly has an 'any' type. TS7031
4 |
5 |
> 6 | function ButtonComponent({onClick, caption, ...otherProps}) {
| ^
7 | return (
8 | <button onClick={onClick} className="button" {...otherProps}>{caption}</button>
9 | );
【问题讨论】:
标签: javascript reactjs typescript