简体   繁体   English

我正在尝试在反应中添加添加搜索过滤器,但我收到此错误

[英]I am trying to add add Search filter in react but I am getting this error

  1. I am trying to add Seach filter using the react, and using json data I am trying to match it with the search term我正在尝试使用反应添加 Seach 过滤器,并使用 json 数据我试图将其与搜索词匹配

  2. Below is my code下面是我的代码

    const App = () => { const [searchTerm, setSearchTerm] = useState([""]) const [query, setQuery] = useState(""); const App = () => { const [searchTerm, setSearchTerm] = useState([""]) const [query, setQuery] = useState("");

    useEffect(() => { const url = "https://60d075407de0b20017108b89.mockapi.io/api/v1/animals"; useEffect(() => { const url = "https://60d075407de0b20017108b89.mockapi.io/api/v1/animals";

     const fetchData = async () => { try { const response = await fetch(url); const json = await response.json(); console.log([json].query); setQuery(json.query); } catch (error) { console.log("error", error); } }; fetchData();

    }, []); }, []);

    return ( <input type='text' placeholder='search....' onChange={event => { setSearchTerm(event.target.value) }} /> return (<input type='text' placeholder='search....' onChange={event => { setSearchTerm(event.target.value) }} />

     { query.filter((val) => { if (searchTerm === "s") { return val } else if (val.name.toLowerCase().includes(searchTerm.toLowerCase())) { return val } else return false }).map((val) => { return ( <div className='user' > <p>{val.name}</p> <p>age: {monthDiff(val.bornAt)} months</p> </div> ); })} </div>

    ); ); }; };

When I try to execute, I am getting this below error can anyone explain why it is happening当我尝试执行时,出现以下错误,任何人都可以解释为什么会发生

> Uncaught TypeError: Cannot read properties of undefined (reading
> 'toLowerCase')

It looks like you're initializing query to a string instead of an array of strings.看起来您正在将query初始化为字符串而不是字符串数组。

Maybe try changing this:也许尝试改变这个:

const [query, setQuery] = useState("");

to

const [query, setQuery] = useState([""]);

Also, your searchTerm is initialized to an array: you might have just mixed those up:)此外,您的searchTerm初始化为一个数组:您可能只是将它们混合在一起:)

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

相关问题 我正在尝试过滤搜索已分页的表 - I am trying to filter search a table that is paginated 我正在尝试向我的 React 应用程序添加路由器 - I am trying to add a router to my react app 我正在尝试创建一个反应网站并在其中添加挂钩,但面临这个错误 - I am trying to create a react website and add hooks in it but face this erros 我正在尝试在反应中实现 menuitem 但出现错误 - I am trying to implement menuitem in react but getting error 尝试在Colfusion中使用JavaScript将src属性添加到img标签时,为什么会出现错误? - Why am I getting an error when trying to add the src attribute to an img tag using JavaScript within Colfusion? 我正在尝试使用 aa for 循环将 object 键值添加到空数组进行过滤,我不断得到一个空数组 - I am trying to add an object key value to an empty array using a a for loop to filter, I keep getting an empty array 我正在尝试下载React js,但是又一次又一次出现此错误? - I am trying to download React js, but I am getting this error again and again? 我正在尝试在 html 中添加一个 for 循环,? 但 for 循环显示错误 - I am trying to add a for loop inside html, ? but the for loop shows error 我想添加一个手风琴,但它没有打开 - I am trying to add an accordion but its not opening 我正在尝试添加关闭功能 - I am trying to add the close function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM