Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Actions
  2. export const ADD_TOAST = "ADD_TOAST";
  3. export const DISMISS_TOAST = "DISMISS_TOAST";
  4.  
  5. // Action Creator
  6. export function addToast(toast) {
  7. toast = { ...toast, id: Math.random() };
  8. return function(dispatch) {
  9. dispatch({
  10. type: ADD_TOAST,
  11. toast
  12. });
  13.  
  14. setTimeout(() => {
  15. dispatch({
  16. type: DISMISS_TOAST,
  17. toast
  18. });
  19. }, toast.timeout || 1500);
  20. };
  21. }
  22.  
  23. // Reducer
  24. const toastInitialState = {
  25. toasts: []
  26. };
  27. function toastReducer(state = toastInitialState, action) {
  28. switch (action.type) {
  29. case ADD_TOAST:
  30. return {
  31. ...state,
  32. toasts: [...state.toasts, action.toast]
  33. };
  34.  
  35. case DISMISS_TOAST:
  36. return {
  37. ...state,
  38. toasts: state.toasts.filter(
  39. toast => toast.id !== action.toast.id
  40. )
  41. };
  42.  
  43. default:
  44. return state;
  45. }
  46. }
  47.  
  48.  
  49. // Dispatching a toast looks like this:
  50. dispatch(addToast({ message: "Logged In!" }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement