最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make a search filter with react and typescript - Stack Overflow

programmeradmin2浏览0评论
  1. Hi I would really like some help on how to make a search filter. I wanna be able to type in some text in the input field so the ul list filter with the same letters.
interface ICrypto {
  id: string;
  name: string;
}

const defaultProps:ICrypto[] = [];

const Dashboard: React.FC = () => {
  const [crypto, setCryptos]: [ICrypto[], (posts: ICrypto[]) => void] = React.useState(defaultProps);
  const [search, setSearch]: [string, (search: string) => void] = React.useState("");

  const handleChange = (e: { target: { value: string; }; }) => {
    setSearch(e.target.value);
  };

  const filteredCoins = crypto.filter(crypto =>
    crypto.name.toLowerCase().includes(search.toLowerCase())
  );

  return (
    <div className="App">
     <ul className="posts">
       <input type="text"></input>
         {crypto.map((crypto) => (
           <li key={crypto.id}>
            <h3>{crypto.id}</h3>
              <p>{crypto.current_price}</p>
              <p>{crypto.symbol}</p>
                <img src={crypto.image} alt="image" />
           </li>
         ))}
     </ul>
  {error && <p className="error">{error}</p>}
</div>
  )
}
  1. Hi I would really like some help on how to make a search filter. I wanna be able to type in some text in the input field so the ul list filter with the same letters.
interface ICrypto {
  id: string;
  name: string;
}

const defaultProps:ICrypto[] = [];

const Dashboard: React.FC = () => {
  const [crypto, setCryptos]: [ICrypto[], (posts: ICrypto[]) => void] = React.useState(defaultProps);
  const [search, setSearch]: [string, (search: string) => void] = React.useState("");

  const handleChange = (e: { target: { value: string; }; }) => {
    setSearch(e.target.value);
  };

  const filteredCoins = crypto.filter(crypto =>
    crypto.name.toLowerCase().includes(search.toLowerCase())
  );

  return (
    <div className="App">
     <ul className="posts">
       <input type="text"></input>
         {crypto.map((crypto) => (
           <li key={crypto.id}>
            <h3>{crypto.id}</h3>
              <p>{crypto.current_price}</p>
              <p>{crypto.symbol}</p>
                <img src={crypto.image} alt="image" />
           </li>
         ))}
     </ul>
  {error && <p className="error">{error}</p>}
</div>
  )
}
Share Improve this question asked Jan 5, 2021 at 16:38 Philip_97Philip_97 231 silver badge4 bronze badges 1
  • which part of your code is not working? – ABOS Commented Jan 5, 2021 at 16:40
Add a ment  | 

2 Answers 2

Reset to default 4
const Dashboard: React.FC = () => {
  const [crypto, setCryptos]: [ICrypto[], (posts: ICrypto[]) => void] = React.useState(defaultProps);
  const [search, setSearch]: [string, (search: string) => void] = React.useState("");

  const handleChange = (e: { target: { value: string; }; }) => {
    setSearch(e.target.value);
  };

  return (
    <div className="App">
     <ul className="posts">
       <input type="text" onChange={handleChange} />
         {crypto.map((crypto) => {
             if (search == "" || crypto.name.toLowerCase().includes(search.toLowerCase())) {
                 return (
                     <li key={crypto.id}>
                         <h3>{crypto.id}</h3>
                         <p>{crypto.current_price}</p>
                         <p>{crypto.symbol}</p>
                         <img src={crypto.image} alt="image" />
                     </li>
                 );
             }
             return null;
         )}
     </ul>
     {error && <p className="error">{error}</p>}
   </div>
  )
}

So the filtering logic looks fine but what are you missing is to set the search input state.

Change this line

<input type="text"></input>

to this:

<input type="text" value={search} onChange={(e) => setSearch(e.target.value)}></input>
发布评论

评论列表(0)

  1. 暂无评论