{"id":2009,"date":"2024-09-06T12:00:00","date_gmt":"2024-09-06T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/react-how-to-filter-select-list-by-another-select-list-in-react\/"},"modified":"2026-07-05T03:26:55","modified_gmt":"2026-07-05T01:26:55","slug":"react-how-to-filter-select-list-by-another-select-list-in-react","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/react-how-to-filter-select-list-by-another-select-list-in-react\/","title":{"rendered":"React \u2013 How to Filter Select list by Another Select list in React"},"content":{"rendered":"<p>In this tutorial, I will show you how to filter a Dropdownlist based on another Dropdownlist in React. We would use the example of Product, Category and SubCategory to demonstrate this.<\/p>\n<p>We would cover the following six steps<\/p>\n<ol>\n<li><a href=\"#t1\">Define\u00a0 the States<\/a><\/li>\n<li><a href=\"#t2\">Create the useEffect Hook to Fetch the Categories<\/a><\/li>\n<li><a href=\"#t3\">Create the useEffect Hook to Fetch the SubCategories<\/a><\/li>\n<li><a href=\"#t4\">Write the Categories &lt;Select&gt; and Handler<\/a><\/li>\n<li><a href=\"#t5\">Write the SubCategories &lt;Select&gt; and Handler<\/a><\/li>\n<li><a href=\"#t6\">Add Some Styling<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Create the States<\/strong><\/h4>\n<p>We would need states to hold the total categories list(categories, the complete subcategory list\u00a0 (allSubcategories) and the filtered sub-category list (filteredSubCategories)<\/p>\n<p>These are defined as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> [categories, setCategories] <span style=\"color: #333333;\">=<\/span> useState([]);\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> [allSubCategories, setAllSubCategories] <span style=\"color: #333333;\">=<\/span> useState([]); \r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> [filteredSubCategories, setFilteredSubCategories] <span style=\"color: #333333;\">=<\/span> useState([]); \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also update the <strong><em>newProduct<\/em><\/strong> state to include <em>categoryid<\/em> and <em>subcategoryid<\/em>. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> [newProduct, setNewProduct] <span style=\"color: #333333;\">=<\/span> useState({\r\n  title<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span>,\r\n  summary<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span>,\r\n  content<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span>,\r\n  categoryid<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span>,\r\n  subcategoryid<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span>,\r\n  created_at<span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #007020;\">Date<\/span>().toISOString()  <span style=\"color: #888888;\">\/\/ Set current datetime<\/span>\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create the useEffect Hook to Fetch Categories<\/strong><\/h4>\n<p>We use the effect hook to load up the Categories when the application starts up.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">useEffect(() <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    axios.get(<span style=\"background-color: #fff0f0;\">'http:\/\/localhost:8081\/categories'<\/span>).then((response) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n      setCategories(response.data)\r\n  })\r\n  .<span style=\"color: #008800; font-weight: bold;\">catch<\/span> ((error) <span style=\"color: #333333;\">=&gt;<\/span>  {\r\n      console.error(<span style=\"background-color: #fff0f0;\">'Error fetching categories:'<\/span>, error);\r\n  });\r\n}, []);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Create the useEffect Hook to Fetch SubCategories<\/strong><\/h4>\n<p>We also want to load up the subCategories when the application starts up.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">useEffect(() <span style=\"color: #333333;\">=&gt;<\/span> {\r\n  axios.get(<span style=\"background-color: #fff0f0;\">'http:\/\/localhost:8081\/subCategories'<\/span>).then((response) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n      setAllSubCategories(response.data);\r\n    })\r\n    .<span style=\"color: #008800; font-weight: bold;\">catch<\/span>((error) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n      console.log(<span style=\"background-color: #fff0f0;\">'Error occurred fetching subcategories'<\/span>, error);\r\n    })\r\n}, []);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Update the Table: At this point you can update the table to include category and subcategory columns<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Write the Categories Select and Handler<\/strong><!-- HTML generated using hilite.me --><\/h4>\n<p>We load the categories into the &lt;select&gt; for categories. We also set the value to newProduct.categoryid. Also not the the default selected item is the text &#8220;Choose..&#8221; whose value defaults to empty string.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;select<\/span> \r\n    <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"categoryid\"<\/span> <span style=\"color: #0000cc;\">onChange=<\/span><span style=\"background-color: #fff0f0;\">{handleCategoryChange}<\/span> \r\n    <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{newProduct.categoryid}<\/span> <span style=\"color: #007700;\">&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;option<\/span> <span style=\"color: #007700;\">&gt;<\/span>Choose...<span style=\"color: #007700;\">&lt;\/option&gt;<\/span>\r\n      {categories.filter(c =&gt; c.title != null).map(category =&gt; (\r\n        <span style=\"color: #007700;\">&lt;option<\/span> <span style=\"color: #0000cc;\">key=<\/span><span style=\"background-color: #fff0f0;\">{category.id}<\/span> <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{category.id}<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n          {category.title}\r\n        <span style=\"color: #007700;\">&lt;\/option&gt;<\/span>\r\n      ))}\r\n<span style=\"color: #007700;\">&lt;\/select&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Write the event handler (handleCategoryChange)<\/strong>: When the category is selected we do the following<\/p>\n<ul>\n<li>obtain the value of the category that was selected<\/li>\n<li>update the categoryid of the newProduct<\/li>\n<li>filter the subCategories base on the categoryid\u00a0<!-- HTML generated using hilite.me --><\/li>\n<\/ul>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> handleCategoryChange <span style=\"color: #333333;\">=<\/span> (event) <span style=\"color: #333333;\">=&gt;<\/span> { \r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> selectedCategoryId <span style=\"color: #333333;\">=<\/span> event.target.value; \r\n  setNewProduct({ \r\n      ...newProduct, \r\n      categoryid<span style=\"color: #333333;\">:<\/span> selectedCategoryId, \r\n      subcategoryid<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">''<\/span> <span style=\"color: #888888;\">\/\/ Reset subcategory when category changes <\/span>\r\n  });\r\n    <span style=\"color: #888888;\">\/\/ Filter the subcategories based on the selected category<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">const<\/span> filteredSubCategories <span style=\"color: #333333;\">=<\/span> allSubCategories.filter(\r\n      (subCategory) <span style=\"color: #333333;\">=&gt;<\/span> subCategory.categoryid <span style=\"color: #333333;\">===<\/span> <span style=\"color: #007020;\">Number<\/span>(selectedCategoryId)\r\n    );\r\n    setFilteredSubCategories(filteredSubCategories); <span style=\"color: #888888;\">\/\/ Update the filtered subcategories<\/span>\r\n} \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Write the SubCategories Select and Handler<\/strong><\/h4>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;select<\/span>\r\n  <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"subcategoryid\"<\/span> <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{newProduct.subcategoryid}<\/span> \r\n  <span style=\"color: #0000cc;\">onChange=<\/span><span style=\"background-color: #fff0f0;\">{handleSubCategoryChange}<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n  <span style=\"color: #007700;\">&lt;option&gt;<\/span>Choose...<span style=\"color: #007700;\">&lt;\/option&gt;<\/span>\r\n  {filteredSubCategories.map(subCategory =&gt; (\r\n    <span style=\"color: #007700;\">&lt;option<\/span> <span style=\"color: #0000cc;\">key=<\/span><span style=\"background-color: #fff0f0;\">{subCategory.id}<\/span> <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{subCategory.id}<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n      {subCategory.description}\r\n    <span style=\"color: #007700;\">&lt;\/option&gt;<\/span>\r\n  ))}\r\n<span style=\"color: #007700;\">&lt;\/select&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the handler for subCategories select, we simply obtain the selected sub-categoryid and use it to update the subcategoryid field of the newProduct<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> handleSubCategoryChange <span style=\"color: #333333;\">=<\/span> (event) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> selectedSubCategoryId <span style=\"color: #333333;\">=<\/span> event.target.value;\r\n  setNewProduct({\r\n    ...newProduct,\r\n    subcategoryid<span style=\"color: #333333;\">:<\/span> selectedSubCategoryId\r\n  });\r\n};\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Add Some Styling to the Select\u00a0<\/strong><\/h4>\n<p>We add some style to the &lt;select&gt; using the sx prop<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  <span style=\"color: #007700;\">style<\/span><span style=\"color: #333333;\">=<\/span>{{\r\n    <span style=\"color: #008800; font-weight: bold;\">width<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'100%'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Makes it full <span style=\"color: #008800; font-weight: bold;\">width<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">padding<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'8px'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Adds <span style=\"color: #008800; font-weight: bold;\">padding<\/span> for better UI\r\n    borderRadius<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'4px'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Adds rounded corners\r\n    <span style=\"color: #008800; font-weight: bold;\">border<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'1px solid #ccc'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Adds a <span style=\"color: #008800; font-weight: bold;\">border<\/span>\r\n    marginBottom<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'16px'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Adds <span style=\"color: #008800; font-weight: bold;\">margin<\/span> at the <span style=\"color: #008800; font-weight: bold;\">bottom<\/span>\r\n    fontSize<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">'16px'<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">\/\/<\/span> Sets a readable <span style=\"color: #008800; font-weight: bold;\">font<\/span> <span style=\"color: #008800; font-weight: bold;\">size<\/span>\r\n  }}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now you can start the application and enjoy!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, I will show you how to filter a Dropdownlist based on another Dropdownlist in React. We would use the example of Product, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-2009","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2009"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2009\/revisions"}],"predecessor-version":[{"id":2177,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2009\/revisions\/2177"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}