this code is working very well but if a person search a doctor using doctor name this time if any doctor name are not match with this input name this time I want to display a message like "Sorry, this name does not exist"

I'm trying but I can't solve the problem. Please give me some advice.

import React, {useState} from 'react';
import Doctordata from './Doctordata'
import { Link } from 'react-router-dom';
import './DoctorSection.css'
const DoctorSection = () => {
  const [data] = useState(Doctordata);
  const [query,setQuery] = useState("");
  document.title = `Our Doctors`
  return (
    <>
   
     <Link to="/"><button className="btnR"> <i className="fa-solid fa-arrow-left"></i> Back to Home</button></Link>
      <div className='search'>
          <input className='searchbox' placeholder="Search by doctor name..." type="text" onChange={e=>setQuery(e.target.value)} />
      </div>


        <div className='wrapper' id="doctor">
            {data.filter((val)=>{
              if(query === null){
                return val
              }
              else if(val.d_name.toLowerCase().includes(query.toLowerCase())){
                return val
              }
              else{
                return false
              }
            
            })
            .map((values) => {
                const { id, src, d_name, d_info, primary } = values;
                return (
                        <div className="doctor" key={id}>
                            <img className="doctor-img" src={src} alt={d_name}/>
                            <span className="d_name"><b>{d_name}</b></span>
                            <span className="d_info">{d_info}</span>
                            <div>
                                   <div><button className="primary" 
                                    onClick={()=>{alert("Call us  now on 000 0000 0000 or 111 1111 1111 for booking an appoimentment")}}>
                                    {primary}</button> </div> 
                            </div>
                        </div>
                );

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

export default DoctorSection

推荐答案

判断filteredData的长度.如果它是0(并且有一个query),那么可以呈现not found消息.

import React, { useState } from "react";
import Doctordata from "./Doctordata";

const DoctorSection = () => {
  const [data] = useState(Doctordata);
  const [query, setQuery] = useState("");

  const filteredData = data.filter(({ d_name }) => {
    if (d_name.toLowerCase().includes(query.toLowerCase())) {
      return true;
    } else {
      return false;
    }
  });

  return (
    <>
      <input
        placeholder="Search by doctor name..."
        type="text"
        onChange={(e) => setQuery(e.target.value)}
      />

      {query && filteredData.length === 0 && (
        <div>Sorry, this name does not exist</div>
      )}

      {filteredData.map(({ id, d_name }) => {
        return <div key={id}>{d_name}</div>;
      })}
    </>
  );
};

export default DoctorSection;

Edit muddy-water-hpd8yt

Javascript相关问答推荐

django无法解析余数:[0] from carray[0]'

Phaser 3 console. log()特定游戏角色的瓷砖属性

如何在Obsidian dataview中创建进度条

colored颜色 检测JS,平均图像 colored颜色 检测JS

虚拟滚动实现使向下滚动可滚动到末尾

PDF工具包阿拉伯字体的反转数字

Reaction Native中的范围滑块

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

在JS中动态创建对象,并将其追加到HTML表中

如何使用画布在另一个内部绘制一个较小但相同的形状,同时保持恒定的边界厚度?

调用特定数组索引时,为什么类型脚本不判断未定义

使用Library chart.js在一个带有两个y轴的图表中绘制两个数据集

Refine.dev从不同的表取多条记录

使用VITE开发服务器处理错误

如何缩小函数中联合返回类型的范围

ReactJS Sweep Line:优化SciChartJS性能,重用wasmContext进行多图表渲染

如何在单击Search按钮时更新Reaction组件?

鼠标进入,每秒将图像大小减小5%

在openstreemap/leaflet中,当鼠标在 map 上移动时,如何在小工具提示中实时显示坐标