I was wonder what I'm doing wrong here. I map through categories and it works perfect. But when I map through sub-categories in dropdown it shows not parent sub-categories, but all items from all categories. For example if I choose category "Developer" in sub categories drop-down I want to show only Front-end, Back-End and Full-Stack options.
setCategory comes from props and the state looks like this:
const [category, setCategory] = useState("");
Code: belox:
<div className="inner--form--row">
<label htmlFor="category">Category:</label>
<select id="category" onChange={(e) => setCategory(e.target.value)}>
<option defaultValue={true}>Choose...</option>
{categories.map((category) => (
<option value={category.label} key={category.id}>
{category.label}
</option>
))}
</select>
</div>
<div className="inner--form--row">
<label htmlFor="subCategory">Sub Category:</label>
<select id="category" onChange={(e) => setSubCategory(e.target.value)}>
<option defaultValue={true}>Choose...</option>
{categories.map((category) => (
<>
{category.subCategories.map((subCat) => (
<option value={subCat.label} key={subCat.id}>
{subCat.label}
</option>
))}
</>
))}
</select>
</div>
Data looks like this:
categories: [
{
id: 1,
label: "Developer",
subCategories: [
{
id: 2,
label: "Front-End Developer",
subCategories: [
{
id: 3,
label: "Junior",
},
{ id: 4, label: "Mid" },
{
id: 5,
label: "Senior",
},
],
},
{
id: 6,
label: "Back-End Developer",
subCategories: [
{ id: 7, label: "Junior" },
{ id: 8, label: "Mid" },
{ id: 9, label: "Senior" },
],
},
{
id: 10,
label: "Full-Stack Developer",
subCategories: [
{ id: 11, label: "Junior" },
{
id: 12,
label: "Mid",
},
{
id: 13,
label: "Senior",
},
],
},
],
},
....
