Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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];
};
Source by dmitripavlutin.com #
 
PREVIOUS NEXT
Tagged: #custom #hook #unique #items #react
ADD COMMENT
Topic
Name
3+8 =