0

I am working on one React.js Application where i need to show checkbox checked ,if user is already present in selected users. I am able to implement that logic ,but the problem is , i have to compare user with thousand of other users to check if user is already in selected users,as it makes my application very slow to compare with thousands of users . How can optimize this List rendering,

const isSelectedArea = (user) => {
    selectedUsers.forEach((u) => {
      if (u.find((a) => a._id === user._id)) {
         return true;
       }
      
    });
  };

<span className={`block border-gray-200 bg-gray-200 ${isSelectedUser(user) ? "check":" "}`}></span>

1 Answer 1

1

You could work with state and an click event handler to save the selected user. Then a simple check to see if the current user id is equal to the selected user id is enough to add the extra css class.

const [selectedUserId, setSelectedUserId] = useState();

const onClick = (userId) => {
  setSelectedUserId(userId)
}

return <div className={`button ... ${selectedUserId === user.id ? 'check' : ''}} onClick={() => onClick(user.id)}>{user.whatever}</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Tomas Vancoillie : selectedUsers is coming from backend initially

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.