【发布时间】:2020-11-19 10:54:16
【问题描述】:
对不起,我想了解并使用Formik 实现一些目标。
我有两个选择字段,我想将它们选择的值传递给 formik 值。他们所做的基本上是获取CountryArray及其对应的区域。我将它们创建为独立组件的原因是,我可以将它们传递给 Formik 中的 Field 组件。我的国家 Array 来自
import countries from "../data/c-r";。我也在使用来自react 的useState。但是我知道在使用 formik 时,您不再需要管理您的状态,cus Formik 可以做到这一点。我怎样才能做到这一点。
const [country, setCountry] = useState("");
const [region, setRegion] = useState("");
const CountryComponent = () => (
<FormControl>
<InputLabel id="demo-simple-select-label">Country</InputLabel>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={country}
onChange={handleChangeCountry}
>
{countries.map(country => (
<MenuItem value={country} key={country.countryShortCode}>
{country.countryName}
</MenuItem>
))}
</Select>
</FormControl>
);
const StateComponent = () => (
<FormControl>
<InputLabel id="demo-simple-select-label">Region</InputLabel>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={region}
onChange={handleChangeRegion}
disabled={!country}
>
{country
? country.regions.map(region => (
<MenuItem value={region} key={region.shortCode}>
{region.name}
</MenuItem>
))
: []}
</Select>
</FormControl>
);
我做了什么...
<Field
type="select"
placeholder="State"
name="state"
as={StateComponent}
fullWidth
label="Select State"
/>
<Field
type="select"
placeholder="Country"
name="country"
as={CountryComponent}
fullWidth
label="Select Country"
/>
问题是我无法在 Formik Value 中获取地区和国家的值,我该怎么做?
谢谢!
【问题讨论】:
标签: reactjs material-ui formik