Simon
Simon

Reputation: 63

How can i filter an array in react and not leave it empty?

I am filtering an array of users based on an input field, but it is not working properly. When writing forward it works but when characters are deleted the array remains the same as the last thing you searched for, it does not get refilled with data as you delete. This I suppose happens because I am changing the state completely and not doing a copy and iterating over that copy, I don't know. How can i fix this? I am using redux.

This is my input onchange function.

  const handleOnChange = (e) => {
    const keyword = e.target.value;
    const res = state.users.filter(u => u.login.includes(keyword));
    dispatch(updateUsers(res));
    setInputValue(keyword);
  };

Upvotes: -1

Views: 782

Answers (1)

user6849815
user6849815

Reputation: 31

In my opinion, if you are rendering state.users and it is getting updated on forwarding search, then when you delete or truncate a string the filtering will be applied to the updated state.users. By setting debugger breakpoints or printing output on the console, you will gain a better understanding.

Upvotes: 1

Related Questions