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/>
}
}