Coding task · JavaScript and React course · Module 15: Patterns and Architecture
Practice: Provider Pattern
The provider hides the state and the logic, and components get only a ready object with the list and the functions through the hook. Change state without mutation: the spread [...prev, newItem] creates a new array with the added item, and filter returns a new array without the removed one. The next id comes from a counter in useRef, so two notifications with the same text can be closed separately. A context without a default value returns undefined outside the provider, so the hook can then throw a readable error instead of letting a component work on empty data.
Task
Finish the notification centre built on the Provider Pattern. ___BLANK1___: addNotification returns a new array with the existing notifications and the new object { id, message, type } at the end. ___BLANK2___: removeNotification keeps every notification except the one with the given id. ___BLANK3___: clearAll sets an empty list. ___BLANK4___: useNotifications throws an error when it is used outside NotificationProvider (useContext then returns undefined).