Answers for "custom hook for unique items in react"

0

custom hook for unique items in react

// useUnique.js
export function useUnique(initial) {
  const [items, setItems] = useState(initial);
  const add = newItem => {
    const uniqueItems = [...new Set([...items, newItem])];
    setItems(uniqueItems);
  };
  return [items, add];
};
Posted by: Guest on July-27-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language