Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback: Sets #148

Open
jackherizsmith opened this issue Nov 15, 2023 · 0 comments
Open

Feedback: Sets #148

jackherizsmith opened this issue Nov 15, 2023 · 0 comments

Comments

@jackherizsmith
Copy link
Contributor

jackherizsmith commented Nov 15, 2023

There is a JS data structure called a set which is quite cool (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set), it behaves a lot like an object but can be used more like arrays.

for example in useAddWorkSpace you have the following code

  const handleAmenityChange = (amenity: string) => {
    if (selectedAmenities.includes(amenity)) {
      setSelectedAmenities(selectedAmenities.filter((a) => a !== amenity));
    } else {
      setSelectedAmenities([...selectedAmenities, amenity]);
    }
  };

using a set, you could rewrite this

  const handleAmenityChange = (amenity: string) => {
    const amenitiesSet = new Set(selectedAmenities);
    if (amenitiesSet.has(amenity)) {
      amenitiesSet.delete(amenity);
    } else {
      amenitiesSet.add(amenity);
    }
    setSelectedAmenities([...amenitiesSet]);
  };

I probably wouldn't bother! but they're handy for deduplicating arrays and quickly adding / removing like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant