I used react-country-region-selector with useState, it worked well and updated the dropdown with my selection of country, code as follows:
import React, { useState } from 'react';
const App = () => {
const [country, setCountry] = useState('');
const handleCountryChange = (country) => {
setCountry(country);
}
return (
<CountryDropdown
value={country}
onChange={handleCountryChange}
/>
)
}
Now I'm trying to use useReducer cause I have multiple states to update. However my code no longer works with react-country-region-selector, code as follows:
import React, { useReducer } from 'react';
const App = () => {
const reducer = (state, action) => {
switch (action.type) {
case 'setCountry':
return {
country: state.country
}
}
}
const handleCountryChange = (country) => {
dispatch({type: 'setCountry'});
}
return (
<CountryDropdown
value={country}
onChange={handleCountryChange}
/>
)
}
When selecting a country, the dropdown no longer updates. What is the matter with useReducer in this case? How can I update the country selection with useReducer?