reactjs - 如何使用反应钩子添加级联下拉菜单?
问题描述
我是新来的反应。我想做一个级联下拉菜单。我使用反应钩子绑定下拉列表的值。我在后端使用了 Web API 和 .net 核心。现在如何根据区域值显示经销商点值?请检查此代码并就如何创建级联下拉列表给我一个建议。提前致谢
这是我的代码:
const [resultZone, lstZone] = useState([]);
useEffect(() => {
fetch('http://localhost:61808/api/Zone', {
method: 'Get',
headers: {
'Content-Type': 'application/json',
}
})
.then(resp => resp.json())
.then(resp => lstZone(resp))
}, [])
const [resultDealer, lstDealer] = useState([]);
useEffect(() => {
if(resultZone){
fetch('http://localhost:61808/api/Dealer', {
method: 'Get',
headers: {
'Content-Type': 'application/json',
}
})
.then(resp => resp.json())
.then(resp => lstDealer(resp))
}
}, [])
return (
<form autoComplete="off" noValidate className={classes.root} onSubmit={handleSubmit}>
<InputLabel style={{ display: 'block' }} className={classes.FormControl}>Dealer Point</InputLabel>
<FormControl variant="outlined"
className={classes.FormControl}>
<Select name="dealerId"
value={values.dealerId}
onChange={handleInputChange}>
<MenuItem value="">Select Dealer</MenuItem>
{
resultDealer.map(x => {
return (
<MenuItem value={x.id}>{x.name}</MenuItem>
)
})
}
</Select>
</FormControl>
<InputLabel style={{ display: 'block' }} className={classes.FormControl}>Zone</InputLabel>
<FormControl variant="outlined"
className={classes.FormControl}>
<Select name="zoneId"
value={values.zoneId}
onChange={handleInputChange}>
<MenuItem value="">Select Zone</MenuItem>
{
resultZone.map(x => {
return (
<MenuItem value={x.id}>{x.name}</MenuItem>
)
})
}
</Select>
</FormControl>
</form >
);
解决方案
推荐阅读
- netsuite - Netsuite - 保存的搜索公式(文本)案例语句连接 2 个字段
- sql - 具有额外列名的动态数据透视表
- android - 在 Android 上使用 WorkManager
- angular - Angular - 使用 NGX-Translate 翻译数据表列名称
- reactjs - Formik 使用 initialValues 来检查带有值的复选框
- python - 我们可以在 Python 中创建持续时间不是 24 小时的自定义时间戳吗?
- javascript - 布局=填充下一个/图像时,我可以获得宽度值吗?
- mongodb - 如何过滤两层深度的数组的数组元素?
- reactjs - 样式组件中组件组合的正确方法是什么
- javascript - 在快速路由器的每个子端点上添加身份验证检查