Guest User

Untitled

a guest
Feb 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import React from "react";
  2.  
  3. let lsBus = {};
  4. let ssBus = {};
  5.  
  6. /**
  7. * Redraw all components that have a hook to localStorage with the given key.
  8. * @param {string} key
  9. * @param {*} newValue
  10. */
  11. const notifyLSBus = (key, newValue) => {
  12. if (!lsBus || !lsBus[key]) {
  13. return;
  14. }
  15. Object.values(lsBus[key]).forEach(u => u(newValue));
  16. };
  17.  
  18. /**
  19. * Redraw all components that have a hook to sessionStorage with the given key.
  20. * @param {string} key
  21. * @param {*} newValue
  22. */
  23. const notifySSBus = (key, newValue) => {
  24. if (!ssBus || !ssBus[key]) {
  25. return;
  26. }
  27. Object.values(ssBus[key]).forEach(u => u(newValue));
  28. };
  29.  
  30. /**
  31. * Hooks into localStorage. The value will be taken from localStorage, if the key exists there.
  32. * If not, the value will use the `initialValue` data. Use the setFunction to update the value inside
  33. * localStorage _and_ notify all components that use the same hook that the value behind the key has changed.
  34. *
  35. * You can pass whatever is JSON encodable to the setFunction - it will take care of storing it correctly.
  36. * @param {string} key
  37. * @param {*} [initialValue=null]
  38. * @returns {Array} [value, setFunction]
  39. */
  40. export const useLocalStorage = (key, initialValue = null) => {
  41. let defaultValue;
  42. try {
  43. defaultValue = localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)) : initialValue;
  44. } catch (e) {
  45. defaultValue = initialValue;
  46. }
  47. const [value, setValue] = React.useState(defaultValue);
  48. const componentId = React.useState(Math.random().toString())[0];
  49.  
  50. React.useEffect(() => {
  51. lsBus[key] = lsBus[key] || {};
  52. lsBus[key][componentId] = setValue;
  53. return () => delete lsBus[componentId];
  54. });
  55.  
  56. return [
  57. value,
  58. (newValue) => {
  59. localStorage.setItem(key, JSON.stringify(newValue));
  60. notifyLSBus(key, newValue);
  61. }
  62. ];
  63. };
  64.  
  65. /**
  66. * Hooks into sessionStorage. The value will be taken from sessionStorage, if the key exists there.
  67. * If not, the value will use the `initialValue` data. Use the setFunction to update the value inside
  68. * sessionStorage _and_ notify all components that use the same hook that the value behind the key has changed.
  69. *
  70. * You can pass whatever is JSON encodable to the setFunction - it will take care of storing it correctly.
  71. * @param {string} key
  72. * @param {*} initialValue
  73. * @returns {Array} [value, setFunction]
  74. */
  75. export const useSessionStorage = (key, initialValue) => {
  76. let defaultValue;
  77. try {
  78. defaultValue = sessionStorage.getItem(key) ? JSON.parse(sessionStorage.getItem(key)) : initialValue;
  79. } catch (e) {
  80. defaultValue = initialValue;
  81. }
  82. const [value, setValue] = React.useState(defaultValue);
  83. const componentId = React.useState(Math.random().toString())[0];
  84.  
  85. React.useEffect(() => {
  86. ssBus[key] = ssBus[key] || {};
  87. ssBus[key][componentId] = setValue;
  88. return () => delete ssBus[componentId];
  89. });
  90.  
  91. return [
  92. value,
  93. (newValue) => {
  94. sessionStorage.setItem(key, JSON.stringify(newValue));
  95. notifySSBus(key, newValue);
  96. }
  97. ];
  98. };
Add Comment
Please, Sign In to add comment