I have a search bar in my React JS application that gets an input and search for it in a table:
const [search, setSearch] = useState('');
const [users, setUsers] = useState([]);
useEffect(async () => {
const response = await getUsers(search);
setUsers(response.data.users);
}, [search]);
.
.
.
<input type='text' className='my-3 py-2 pl-3 pr-10 text-sm' placeholder='search term..' onChange={(e) => setSearch(e.target.value)} value={search} />
It works fine. Now I want to make it possible that when my string contains a special character, replace that character with another one and search for the new term. For example, if my string is hello
, I want to change h
to m
and search for mello
. I changed my code to this:
useEffect(async () => {
if (search.indexOf('h') > -1)
{
alert('Found');
search = search.replace("h", "m");
}
const response = await getUsers(search);
setUsers(response.data.users);
}, [search]);
Now when input string contains h
, I receive an alert but the searching process is not working correctly (it does not search for anything). What can I do?