I have a simple weather app built using React. The App.js
code includes the following in its return()
statement:
<Routes>
<Route exact path="/" element={<Form />} />
<Route exact path="/weather/*" element={<Weather />} />
</Routes>
Form.jsx
takes in user input, sets as State, and looks like:
export default function Form() {
const [zipCode, setZipCode] = useState('');
const getZip = (e) => {
e.preventDefault();
setZipCode(e.target[0].value);
}
return (
<div>
<form onSubmit={getZip}>
<input id="zip" />
<label htmlFor="zip">Your Zip Code</label>
<Link to="/Weather" role="button" type="submit" className="btn btn-primary">Enter</Link>
</form>
)}
Weather.jsx
is supposed to take that zip code and make an API call to get weather data:
function Weather() {
const { zipCode } = useParams();
const [forecastObj, setForecastObj] = useState({});
useEffect(() => {
fetch(`https://apiservice.com/forecast/loc=${zipCode}`)
}
The app is supposed to display Form.jsx
, and switch to Weather.jsx
once the zip code is submitted. When I input a zip code I get an error: GET https://apiservice.com/forecast/loc=undefined 400
, so the zip code isn't being passed to Weather.jsx
. Most of the resources I've found through googling seem to be for older versions (lots of this.state = state
or similar). Other resources say to use useParams
, which I have set up in Weather.jsx
.
What am I missing?