You need to fetch the data from that endpoint, using fetch and the following approach could help:
function Form(){
const [targetValue, setTargetValue] = useState('');
async function callWeatherAPI(city){
const response = await fetch(`endpoint/${city}`);
return await response.json(); // You will probably receive an object, get the value you want and return it here
}
async function submitForm() {
// You should get the form value using reactivity..
const city = document.forms["myForm"]["city"].value;
const isValid = validateForm(city);
if (isValid) {
const response = await callWeatherAPI(city);
setTargetValue(response);
} else {
alert("Please put in value for city");
}
}
return (
<form name="myForm" onSubmit={submitForm}>
<label>City:</label>
<input type="text" name="city"></input>
<input type="submit"></input>
<p>Response: {targetValue}</p>
</form>
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…