Drop down filtering with react

toufiqulhoque

SM Toufiqul Hoque

Posted on February 26, 2022

Drop down filtering with react

Our goal is to change our data dynamically using Drop down filtering here is the code ...

import React, { useState } from 'react';
import mockData from '../mockData.json';


const Dropdown = () => {
  const [items, setItems] = useState(mockData);

   const filterItem=(gen)=>{
     const updatedItem=mockData.filter((curElem)=>{
       return curElem.gender===gen
     });
     setItems(updatedItem)

   }
  return (
    <div>

        <select onChange={(e) => filterItem(e.target.value)}  > 
          <option >Female</option>
          <option>Male</option>

        </select>


        {items.map(val => (
          <div key={val.id} style={{ margin: '30px' }}>
            <div>{`Band: ${val.first_name}`}</div>
            <div>{`Albums: ${val.last_name}`}</div>
            <div>{`Members: ${val.email}`}</div>
            <div>{`Members: ${val.gender}`}</div>

          </div>
        ))}
      </div>
  );
}

export default Dropdown;
Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
toufiqulhoque
SM Toufiqul Hoque

Posted on February 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related