Skip to content

Commit

Permalink
fix(clayui.com): Add sanity check if the code is in SSR for window ob…
Browse files Browse the repository at this point in the history
…ject
  • Loading branch information
kresimir-coko authored and bryceosterhaus committed Aug 12, 2020
1 parent 6e53c49 commit 797dd1e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions clayui.com/src/components/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,30 @@ const Editor = ({
}

function useStickyState(defaultValue, key) {
const inBrowser = typeof window !== 'undefined';

const localStorage = {
getItem(key) {
return inBrowser ? window.localStorage.getItem(key) : null;
},

setItem(key, value) {
if (inBrowser) {
window.localStorage.setItem(key, value);
}
},
};

const [value, setValue] = React.useState(() => {
const stickyValue = window.localStorage.getItem(key);
const stickyValue = localStorage.getItem(key);

return stickyValue !== null
? JSON.parse(stickyValue)
: defaultValue;
});

React.useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(value));
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);

return [value, setValue];
Expand Down

0 comments on commit 797dd1e

Please sign in to comment.