进行搜索后如何显示(找不到结果)

发布于 2025-02-11 14:36:45 字数 3588 浏览 1 评论 0原文

问题:

我想在进行搜索后显示(找不到结果)并且没有找到任何东西,我知道我应该处理错误,但我不知道如何!初学者的事情。

代码:

首先,我获取了数据,然后我作为apidata定居,然后在用户键入时开始过滤结果,但我想显示一个(找不到结果)。

export default function SearchInput() {

    const [APIData, setAPIData] = useState([]);
    const [searchInput, setSearchInput] = useState('');
    const [filteredResults, setFilteredResults] = useState([]);

    const [error, setError] = useState(false);
    const [isLoaded, setIsLoaded] = useState(false);
    
    useEffect(() => {
        const fetchData = async () => {
          setError(false);
          setIsLoaded(true);
    
          try{
          const response = await axios("/posts");
          setAPIData(response.data);
          } catch(error) {
            setError(true);
            console.error(error);
          }
          setIsLoaded(false);
        };
        fetchData();
      }, []);
      console.log(APIData)
    
    const searchItems = (searchValue) => {
        setSearchInput(searchValue)
        if (searchInput !== '') {
            const filteredData = APIData.filter((post) => {
                return Object.values(post).join('').toLowerCase().includes(searchInput.toLowerCase())
            })
            setFilteredResults(filteredData)
        }
        else{
            setFilteredResults(APIData)
        }
    }

    return (
        
        <div style={{ padding: 20 }}>
            
            <OutlinedInput 
                className="SearchInput"
                placeholder='Search...'
                onChange={(e) => searchItems(e.target.value)}
                endAdornment={
                    <InputAdornment>
                    <SearchIcon />
                    </InputAdornment>
                }
            />

            <div style={{ marginTop: 20 }}>

                {searchInput.length > 0 ? (

                    filteredResults.map((post) => {
                        return (
                            <div className="card" key={post._id}>
                            <div className="card-content">
                            <Link to={`/post/${post._id}`} className="link">
                            <h2 className="results-title">
                                {post.title}
                            </h2>
                            <ol className="card-username">
                                {post.username}      
                            </ol>
                            </Link>
                            </div>
                        </div>
                        )
                    })
                ) : (
                    APIData.map((post) => {
                        return (
                            <article className="card" key={post._id}>
                            <div className="card-content">
                            <Link to={`/post/${post._id}`} className="link">
                            <h2 className="card-name">
                                {post.title}
                            </h2>
                            <ol className="card-username">
                                {post.username}      
                            </ol>
                            </Link>
                            </div>
                        </article>
                        )
                    })
                )}
            </div>
        </div>

The problem:

I want to show a (no results found) after doing a search and didn't found anything, I know that I should handle errors but I didn't know how! Beginners thing.

The Code:

first I fetched the data, then I settled as APIData, then when user is typing it start to filter the results, but i want to show a (no results found).

export default function SearchInput() {

    const [APIData, setAPIData] = useState([]);
    const [searchInput, setSearchInput] = useState('');
    const [filteredResults, setFilteredResults] = useState([]);

    const [error, setError] = useState(false);
    const [isLoaded, setIsLoaded] = useState(false);
    
    useEffect(() => {
        const fetchData = async () => {
          setError(false);
          setIsLoaded(true);
    
          try{
          const response = await axios("/posts");
          setAPIData(response.data);
          } catch(error) {
            setError(true);
            console.error(error);
          }
          setIsLoaded(false);
        };
        fetchData();
      }, []);
      console.log(APIData)
    
    const searchItems = (searchValue) => {
        setSearchInput(searchValue)
        if (searchInput !== '') {
            const filteredData = APIData.filter((post) => {
                return Object.values(post).join('').toLowerCase().includes(searchInput.toLowerCase())
            })
            setFilteredResults(filteredData)
        }
        else{
            setFilteredResults(APIData)
        }
    }

    return (
        
        <div style={{ padding: 20 }}>
            
            <OutlinedInput 
                className="SearchInput"
                placeholder='Search...'
                onChange={(e) => searchItems(e.target.value)}
                endAdornment={
                    <InputAdornment>
                    <SearchIcon />
                    </InputAdornment>
                }
            />

            <div style={{ marginTop: 20 }}>

                {searchInput.length > 0 ? (

                    filteredResults.map((post) => {
                        return (
                            <div className="card" key={post._id}>
                            <div className="card-content">
                            <Link to={`/post/${post._id}`} className="link">
                            <h2 className="results-title">
                                {post.title}
                            </h2>
                            <ol className="card-username">
                                {post.username}      
                            </ol>
                            </Link>
                            </div>
                        </div>
                        )
                    })
                ) : (
                    APIData.map((post) => {
                        return (
                            <article className="card" key={post._id}>
                            <div className="card-content">
                            <Link to={`/post/${post._id}`} className="link">
                            <h2 className="card-name">
                                {post.title}
                            </h2>
                            <ol className="card-username">
                                {post.username}      
                            </ol>
                            </Link>
                            </div>
                        </article>
                        )
                    })
                )}
            </div>
        </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文