reactjs - 不在反应选择中呈现选择值
问题描述
我已经创建了 3 个条件列表,react-select
但第三个元素无法呈现所选值,我正在尝试更新它,useEffect
但我不知道它是否正确。
我的代码示例:
import React, { useState, useEffect } from "react";
import Select from "react-select";
import data from "../data.json";
const Dropdowns = ()=>{
const [product, setProduct] = useState(null);
const [type, setType] = useState(null);
const [typesList, setTypesList] = useState(null);
const [title, setTitle] = useState(null);
const [titlesList, setTitlesList] = useState(null);
// handle change event of the product dropdown
const handleProductChange = (obj) => {
setProduct(obj);
setTypesList(obj.types)
setType(null);
};
// handle change event of the types dropdown
const handleTypesChange = (obj) => {
setType(obj);
setTitlesList(obj.titles);
};
// handle change event of the titles dropdown
const handleTitlesChange = (obj) => {
setTitle(obj);
};
useEffect( () => {
if ( title ) {
setTitle(title)
}
}, [title])
return (
<>
<br />
<h3>Products</h3>
<Select
placeholder="Select Product"
value={ product }
options={ data }
onChange={ handleProductChange }
getOptionLabel={ (x) => x.product }
/>
<br />
<h3>Types</h3>
<Select
placeholder="Select Types"
value={ type }
options={ typesList }
onChange={ handleTypesChange }
getOptionLabel={ (x) => x.type }
/>
<br />
<h3>Titles</h3>
<Select
placeholder="Select Title"
value={ title }
options={ titlesList }
onChange={ handleTitlesChange }
getOptionLabel={ (x) => x }
/>
</>
)
}
export default Dropdowns;
非常感谢您的帮助!
解决方案
推荐阅读
- rust - 在 Rust 的一行中定义和分配一个二维数组 a
- c# - 如何在 .NET Core 中使用 RSAPSS 算法创建 RSA 实例?
- bootstrap-4 - 如何在模态引导程序中呈现下拉菜单
- android - 使用 androidx.hilt 设置 Android Hilt
- apache - 无法从浏览器访问 Apache 测试页面
- c# - MongoDB .NET 驱动程序映射值为 null
- json - 定义密钥对的数量然后填充它们 json
- r - 什么聚类方法会给出最大的剪影分数?
- python - 如何从另一个命令终止 discord.py 中的异步函数
- c# - 如何查询名称包含一个或多个字符串列表的项目(以转换为 SQL 的方式)?