Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // src/contexts/BookContext.js
  2.  
  3. import React, { createContext, useState } from 'react';
  4. import uniqid from 'uniqid';
  5.  
  6. export const BookContext = createContext();
  7.  
  8. const BookContextProvider = ({ children }) => {
  9. const [books, setBooks] = useState([
  10. {
  11. id: 1,
  12. title: 'Goodnight Moon',
  13. author: 'Margaret Brown'
  14. },
  15. {
  16. id: 2,
  17. title: 'The Very Hungry Caterpillar',
  18. author: 'Eric Carle'
  19. }
  20. ]);
  21.  
  22. const addBook = book => {
  23. setBooks([...books, { ...book, id: uniqid() }]);
  24. };
  25.  
  26. const deleteBook = id => {
  27. setBooks(books.filter(book => book.id !== id));
  28. };
  29.  
  30. return (
  31. <BookContext.Provider value={{ books, addBook, deleteBook }}>
  32. {children}
  33. </BookContext.Provider>
  34. );
  35. };
  36.  
  37. export default BookContextProvider;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement