我有一些关于 react-country-region-selector 的代码,它假设允许我选择国家和地区,但国家没有被选中,地区仍然是一个破折号。有人能告诉我我的代码出了什么问题吗? P.s 我正在使用这个网站作为我的代码,https://github.com/country-regions/react-country-region-selector
import { CountryDropdown, RegionDropdown } from 'react-country-region-selector';
import React, { useState } from "react";
const Locations = () => {
const [state, setState] = useState({
country: "",
region: ""
})
const selectCountry = (val) => {
setState({ state: val });
}
const selectRegion = (val) =>{
setState({ state: val });
}
const { country, region } = setState;
return(
<div>
<CountryDropdown
value={country}
onChange={(val) => selectCountry(val)} />
<RegionDropdown
country={country}
value={region}
onChange={(val) => selectRegion(val)} />
</div>
</div>
);
}
return default Locations;
我目前拥有的:
答案 0 :(得分:0)
你只需要做一些小改动。
const selectCountry = (val) => {
setState({ country: val });
}
const selectRegion = (val) =>{
setState({ region: val });
}
const { country, region } = state;
return(
<div>
<CountryDropdown
value={country}
onChange={(val) => selectCountry(val)} />
<RegionDropdown
country={country}
value={region}
onChange={(val) => selectRegion(val)} />
</div>
</div>