进行搜索后如何显示(找不到结果)
问题:
我想在进行搜索后显示(找不到结果)并且没有找到任何东西,我知道我应该处理错误,但我不知道如何!初学者的事情。
代码:
首先,我获取了数据,然后我作为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论