无法在主页中使用自定义组件-打字稿错误

问题描述 投票:0回答:1

我正在尝试制作自定义警报组件:

type Severity = "error" | "success" | "info" | "warning" | undefined;

export default function CustomAlert(severity: Severity, text: string){
    return(
    <Alert style={{width:'50%'}} severity={severity}> {text}</Alert>)
}

但是我无法在主页功能中使用它:

  function StatusMessage(){
        if (errorMessage.includes(`Couldn't find user`)){
          return (
            <div>
            <CustomAlert severity='error' text='User Not Found'></CustomAlert>
            </div>
              )
  }

自定义警报给我这个错误:

Type '{ severity: string; text: string; }' is not assignable to type '(IntrinsicAttributes & "success") | (IntrinsicAttributes & "info") | (IntrinsicAttributes & "warning") | (IntrinsicAttributes & "error")'.
  Type '{ severity: string; text: string; }' is not assignable to type '"error"'.ts(2322)
javascript reactjs typescript material-ui react-component
1个回答
2
投票

功能组件接收一个参数:props。您可以使用整个对象或对其进行分解,但是不能传递多个对象。

function CustomAlert({severity: Severity, text: string})
function CustomAlert(props: {severity: Severity, text: string})
© www.soinside.com 2019 - 2024. All rights reserved.