I need to populate a select input with a large list of countries. Because of internationalization, I have a separate json file for each language available. In order not to load all of them in my component, I would like to only import the one corresponding to the current locale.
Here is what I wrote:
function MyComponent() {
const [countries, setCountries] = useState([]);
const { locale } = useLocale();
useEffect(() => {
import(`../../data/countries/${locale}.json`)
.then((res) => setCountries(res.countries))
.catch(_ => null);
}, []);
return <Select options={countries}/>
}
I also thought about writing my own hook in a separate file:
export const useFetchJSON = (file: string) => {
const [data, setData] = useState({});
const { locale } = useLocale();
const res = require(`../data/${file}/${locale}.json`);
setData(res);
return data;
};
None of this techniques work. How to fix this? Thanks!