简体   繁体   English

为什么我在 React 中的过滤器 function 不起作用?

[英]Why is my Filter function in React not working?

Hi im trying to add a filter option.I have a list of Restaurant cards....each card has category either alc,coffeeshop or bar.嗨,我正在尝试添加过滤器选项。我有一个餐厅卡列表....每张卡都有 alc、咖啡店或酒吧的类别。 I wanted to add a filter where in I can choose all option and get all restaurants..and if i choose any of the category then those restos.Here is what I have written although not working whats going wrong?我想添加一个过滤器,我可以在其中选择所有选项并获取所有餐厅..如果我选择任何类别,那么那些restos.Here is what I have written but not working 出了什么问题? Restaurants.js餐厅.js

import MyMap from "./MyMap";
import Rcard from "./Rcard";
import Filter from "./Filter";

import { useEffect, useState } from "react";
function Restaurants(){
    const[restaurants,setRestaurants]=useState([]);
    const[filter,setFilter]=useState("");
    useEffect(()=>{
        fetch('/restaurants')
        .then(res=>res.json())
        .then(rdata=>{
            setRestaurants(rdata)
        })
    },[])

    function handleFilter(e) {
        e.target.value === "All"
          ? setFilter("")
          : setFilter(e.target.value);
      }
    
      let displayRestaurants = restaurants;
      if (filter) {
        displayRestaurants = displayRestaurants.filter((r) => {
          return r.type >= filter;
        });
      }
     
    return(
        <>
   
        <MyMap />
        <h1>Restos</h1>
        <Filter  handleFilter={handleFilter}/>
        
        {
            restaurants.map((restaurant)=>(
                <Rcard key={restaurant.id} restaurant={restaurant}/>
            ))
        }

        </>
    )
}
export default Restaurants;

Filter.js过滤器.js

function Filter(handleFilter){
    return(
        <>
        <div>
      <h4> Filter </h4>
      <select
       onChange={handleFilter}
     
      >
        <option>All</option>
        <option value="alc">Fine Dinning</option>
        <option value="coffeeshop">Cafe</option>
        <option value="bar">Pub</option>
        
      </select>
    </div>
        </>
    )
}
export default Filter;

Rcard.js Rcard.js


function Rcard({restaurant}){
    const{ id,name, cuisine,image, cost, address, hours, closed, phone, must_try, category,website}=restaurant;
    return(
        <>
        <img src={image } alt="pic"/>
        <h2>{name}</h2>
        <h3>{cuisine}</h3>
        <h3>{cost}</h3>
        <h3>{address}</h3>
        <h3>{hours}</h3>
        <h3>{closed}</h3>
        <h4>{phone}</h4>
        <h4>{must_try}</h4>
        <h4>{category}</h4>
        <a href={`/restaurants/${id}`}>View</a>
        <a href={website} target="_blank"  rel="noreferrer"  >Website</a>
        
        </>
    )
}
export default Rcard;

Thanks谢谢

  1. You need to check if the filters are applied and this can be done inside the useEffect with the filter as dep array...您需要检查是否应用了过滤器,这可以在 useEffect 内完成,过滤器作为 dep 数组...

     useEffect(()=>{ const filtered = restaurants.filter((r) => { return r.type == filter; // check the condition here }) setRestaurants(filtered) },[filter])
  2. or we can even derive from the state as或者我们甚至可以从 state 派生为

     const filtered = restaurants.filter((r) => { return r.type == filter; // check the condition here })

    use this filtered to map and render使用这个filtered到 map 并渲染

    filtered.map((restaurant)=>( <Rcard key={restaurant.id} restaurant={restaurant}/> ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM