【发布时间】:2022-01-06 20:21:37
【问题描述】:
我这里有一个组件,它获取一个 ID 号并检查该 ID 号是否存在于数据库中。如果它存在,它应该呈现一个组件,如果不存在则显示错误消息。正在为如何编写而苦苦挣扎,不胜感激。
function PatientIDInput({component}) {
const [patientID, setPatientID] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [showComponent, setShowComponent] = useState(false);
const [patientInformation, setPatientInformation] = useState('');
const handleChange = (e) => {
// some input change handling
}
const handleSubmit = (e) => {
e.preventDefault();
Physician.fetchPatientData(patientID)
.then(res => {
if (res.status === 200) {
// show component if patient exists
}
})
.catch(error => {
if (error.response.status) {
// set error if not
}
})
}
return (
<>
<form onSubmit={handleSubmit}>
// some UI code for user's input
</form>
{ showComponent
? {Component patientInformation={patientInformation}} /> // this is where I'm
struggling with the syntax. How should I write the logic? I want to pass
some props to the component too.
: null}
</>
);
}
export default PatientIDInput;
【问题讨论】:
标签: javascript reactjs frontend react-component web-frontend