Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何从接口获取值并将其用于另一个函数_Reactjs_Typescript - Fatal编程技术网

Reactjs 如何从接口获取值并将其用于另一个函数

Reactjs 如何从接口获取值并将其用于另一个函数,reactjs,typescript,Reactjs,Typescript,我无法获取userType的值以在条件satatement上使用它 用户类型: const RadioForm = (props) => { return ( <div> <label>Customer</label> <input type="radio" value="Custom

我无法获取
userType
的值以在条件satatement上使用它

用户类型:

const RadioForm = (props) => {
    return (
        <div>
            <label>Customer</label>
            <input
                type="radio"
                value="Customer"
                checked={props.userType === "Customer"}
                onChange={(e) => {props.setUserType(e.target.value)}}
            />

            <br />

            <label>Vendor</label>
            <input
                type="radio"
                value="Vendor"
                checked={props.userType === "Vendor"}
                onChange={(e) => {props.setUserType(e.target.value)}}
            />
        </div>
    )
}
但我总是犯这样的错误:

此条件将始终返回“false”,因为类型“{if(userType:any):any;}”和“string”没有重叠

回答@Raja

我已经在登录中定义了它:

const [userType, setUserType] = useState("Admin");

  return (
    <LoginPage
      userType={userType}
      setUserType={setUserType}
    />
  );
const[userType,setUserType]=useState(“Admin”);
返回(
);

看起来您将对象定义为文本语法,返回有点错误,期望GetUserType应该是函数组件,而props类型应该是LoginCardProps

  const GetUserType = ({userType}: LoginCardProps) => {
        if (userType === "Customer") {
          return <Customer />
        } else if (userType === "Vendor") {
          return <Vendor/>
        }
      }
const GetUserType=({userType}:LoginCardProps)=>{
如果(用户类型==“客户”){
返回
}else if(用户类型==“供应商”){
返回
}
}

看起来您将对象定义为文本语法,返回有点错误,期望GetUserType应该是函数组件,而props类型应该是LoginCardProps

  const GetUserType = ({userType}: LoginCardProps) => {
        if (userType === "Customer") {
          return <Customer />
        } else if (userType === "Vendor") {
          return <Vendor/>
        }
      }
const GetUserType=({userType}:LoginCardProps)=>{
如果(用户类型==“客户”){
返回
}else if(用户类型==“供应商”){
返回
}
}

能否请您更新问题以重现问题,我无法获取,能否请您更新问题以重现问题,我无法获取
Type '{}' is missing the following properties from type 'LoginCardProps': userType, setUserType
const [userType, setUserType] = useState("Admin");

  return (
    <LoginPage
      userType={userType}
      setUserType={setUserType}
    />
  );
  const GetUserType = ({userType}: LoginCardProps) => {
        if (userType === "Customer") {
          return <Customer />
        } else if (userType === "Vendor") {
          return <Vendor/>
        }
      }