Reactjs 属性“label”在类型“{handleClick:()=>void;}”中丢失,但在类型“{handleClick:()=>void;label:string;}”中是必需的

Reactjs 属性“label”在类型“{handleClick:()=>void;}”中丢失,但在类型“{handleClick:()=>void;label:string;}”中是必需的,reactjs,typescript,types,jsx,next.js,Reactjs,Typescript,Types,Jsx,Next.js,我在描述道具类型时遇到问题。我在以下代码行中遇到错误: <PostButton handleClick={props.upvote}/> <PostButton2 handleClick={props.downvote}/> 错误是: 类型“{handleClick:=>void;}”中缺少属性“label”,但 类型{handleClick:=>void;标签:string; }'.ts2741 Comments.tsx62,55:'标签'在此处声明 当我试图删

我在描述道具类型时遇到问题。我在以下代码行中遇到错误:

 <PostButton handleClick={props.upvote}/>
  <PostButton2 handleClick={props.downvote}/>
错误是:

类型“{handleClick:=>void;}”中缺少属性“label”,但 类型{handleClick:=>void;标签:string; }'.ts2741 Comments.tsx62,55:'标签'在此处声明

当我试图删除'label:string;'从我的函数道具中,它抛出一个错误,声明:

属性“label”在类型“{handleClick:=>void”上不存在; }"

这是我的密码:

function Button(props: { handleClick: () => void;  }) {

    let style = {
        paddingRight: 100,

    };
    return (
        <div>
            <button style={style} onClick={() => props.handleClick()}>
                {props.label}

                    PostButton
            </button>
        </div>
    );
}
您的两个PostButton都使用{props.label}作为回报,所以您必须将label作为props传递

function PostButton(props: { handleClick: () => void; title: string  }) {....}

<PostButton handleClick={props.upvote} title="titlePostButton"/>

<PostButton2 handleClick={props.downvote} title="titlePostButton2"/>

在函数PostButton中,您试图打印{props.label},而该{props.label}不存在于这些道具中,请尝试删除这些道具:{handleClick:=>void;},然后只使用函数PostButton。我看到太多的处理程序被分配为什么都不做,为什么不删除它们并清理代码呢

更好的解决方案: 如果您想继续使用{props.label},只需在调用函数时添加它,如下所示: