Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

search filter in react js using api function components

import React, { useState } from 'react';

import './App.css';

// This holds a list of some fiction people
// Some  have the same name but different age and id
const USERS = [
  { id: 1, name: 'Andy', age: 32 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Tom Hulk', age: 40 },
  { id: 4, name: 'Tom Hank', age: 50 },
  { id: 5, name: 'Audra', age: 30 },
  { id: 6, name: 'Anna', age: 68 },
  { id: 7, name: 'Tom', age: 34 },
  { id: 8, name: 'Tom Riddle', age: 28 },
  { id: 9, name: 'Bolo', age: 23 },
];

function App() {
  // the value of the search field 
  const [name, setName] = useState('');

  // the search result
  const [foundUsers, setFoundUsers] = useState(USERS);

  const filter = (e) => {
    const keyword = e.target.value;

    if (keyword !== '') {
      const results = USERS.filter((user) => {
        return user.name.toLowerCase().startsWith(keyword.toLowerCase());
        // Use the toLowerCase() method to make it case-insensitive
      });
      setFoundUsers(results);
    } else {
      setFoundUsers(USERS);
      // If the text field is empty, show all users
    }

    setName(keyword);
  };

  return (
    <div className="container">
      <input
        type="search"
        value={name}
        onChange={filter}
        className="input"
        placeholder="Filter"
      />

      <div className="user-list">
        {foundUsers && foundUsers.length > 0 ? (
          foundUsers.map((user) => (
            <li key={user.id} className="user">
              <span className="user-id">{user.id}</span>
              <span className="user-name">{user.name}</span>
              <span className="user-age">{user.age} year old</span>
            </li>
          ))
        ) : (
          <h1>No results found!</h1>
        )}
      </div>
    </div>
  );
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get ip address in javascript 
Javascript :: search string in file node 
Javascript :: cypress type force 
Javascript :: javascript take any number of arguments 
Javascript :: javascript check if is array 
Javascript :: node js get file name without extension 
Javascript :: use js variable in blade route 
Javascript :: convert string to datetime javascript 
Javascript :: javascript to string 
Javascript :: how to hide all fo the paragraphs in jquery 
Javascript :: javascript push in specific index 
Javascript :: regex char any quantity 
Javascript :: @angular/common 
Javascript :: array has object with property js 
Javascript :: javascript is variable a string 
Javascript :: javascript regex vowel 
Javascript :: vue read url 
Javascript :: regular expression for password 
Javascript :: npx create react app Must use import to load ES Module error 
Javascript :: remove whitespace javascript 
Javascript :: status nodejs 
Javascript :: counting duplicates codewars javascript 
Javascript :: get value from another textinput and set is to another using jquery 
Javascript :: drupal 8 render node programmatically 
Javascript :: javascript fillstyle 
Javascript :: how select start from id in jquery 
Javascript :: nodejs how cpu handle worker_threads 
Javascript :: rendering htmnl element to DOM 
Javascript :: defer parsing of javascript wordpress 
Javascript :: mongoose connect to URL of atlas 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =