Open shahidforsdlc opened 4 months ago
do you have some info about this bug? I am facing the same problem
I've resolved the issue by creating my own package called usa-state-city
,
as I only need information about the USA for my project. The problem was
occurring in a Vite project using the country-state-city
package, and it
only showed a "maximum call stack error" on iPhone. This error was due to
the large JSON file used by the package, which contains information for all
countries.
If you're using Vite instead of CRA and encountering similar issues, I suggest either using CRA scripts or creating your own package for specific country information. This way, you'll only include the necessary JSON data for that country, rather than the entire world's data.
Hope it helps ,What do you think ?
On Wed, 10 Jul, 2024, 7:34 pm Martín Fenocchio, @.***> wrote:
do you have some info about this bug? I am facing the same problem
— Reply to this email directly, view it on GitHub https://github.com/harpreetkhalsagtbit/country-state-city/issues/184#issuecomment-2220601717, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6L2VDNLW42B66MGQAAIZLTZLU5PXAVCNFSM6AAAAABJ5UBIQSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRQGYYDCNZRG4 . You are receiving this because you authored the thread.Message ID: @.***>
Getting this too, so bizzare - deffo what @shahidforsdlc large non-async JSON imports on mobile cause memory issue, will try an async approach and revert back on this issue.
I fixed it this way with async:
const getCountries = async () => {
const Country = await import('country-state-city/lib/country');
const {getAllCountries} = Country.default;
return getAllCountries().sort((a, b) => a.name.localeCompare(b.name));
};
const getCities = async (country: string, state?: string) => {
const City = await import('country-state-city/lib/city');
const {getCitiesOfState, getCitiesOfCountry} = City.default;
return state ? getCitiesOfState(country, state) : getCitiesOfCountry(country) ?? [];
}
Since it's now async, becomes troublesome as need to run a side affect in react, so used a hook:
import { ICity, ICountry, IState } from 'country-state-city/lib/interface';
export const useCountriesAndCities = (props?: {
country?: string,
state?: string
}) => {
const [countries, setCountries] = useState<ICountry[]>([]);
const [cities, setCities] = useState<ICity[]>([]);
useEffect(() => {
getCountries().then(setCountries);
}, []);
useEffect(() => {
if (props?.country && props?.state) {
getCities(props.country, props.state).then(setCities);
}
}, [props?.country, props?.state]);
const phoneCodes = useMemo(() => countries
.map((country) => ({
value: country.phonecode.startsWith('+')
? country.phonecode
: `+${country.phonecode}`,
label: `${country.flag} ${
country.phonecode.startsWith('+')
? country.phonecode
: `+${country.phonecode}`
}`,
}))
.filter((a, i, arr) => arr.findIndex((b) => b.value === a.value) === i), [countries]);
return {
countries,
cities,
phoneCodes
};
};
Related: https://github.com/harpreetkhalsagtbit/country-state-city/issues/173 https://github.com/harpreetkhalsagtbit/country-state-city/issues/123
This issue still exists. I don't think this package is being managed anymore...