-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '8a789df662dfa582adbf38f59afaf6ad399da79a' into update-v…
…8.4.0-from-upstream
- Loading branch information
Showing
15 changed files
with
1,103 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/components/LayoutWrapperAccountSettingsSideNav/hookGlobalState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
// Global state is useful in situations, where UI state needs to be tracked navigation history. | ||
// Alternatively, Redux could be used too, but it's unnecessarily heavy to use for | ||
// a single component to track it's internal UI state (e.g. "scroll position"). | ||
// If more than one component is needing certain info, the recommendation is to use Redux. | ||
// | ||
// This piece of code is taken from Daishi Kato's blog: | ||
// https://blog.axlight.com/posts/steps-to-develop-global-state-for-react/ | ||
export const createGlobalState = initialState => { | ||
let globalState = initialState; | ||
const listeners = Object.fromEntries(Object.keys(initialState).map(key => [key, new Set()])); | ||
|
||
const setGlobalState = (key, nextValue) => { | ||
globalState = { ...globalState, [key]: nextValue }; | ||
listeners[key].forEach(listener => listener()); | ||
}; | ||
|
||
const useGlobalState = key => { | ||
const [state, setState] = useState(globalState[key]); | ||
useEffect(() => { | ||
const listener = () => { | ||
setState(globalState[key]); | ||
}; | ||
listeners[key].add(listener); | ||
listener(); // in case it's already changed | ||
return () => listeners[key].delete(listener); // cleanup | ||
}, []); | ||
return [state, nextValue => setGlobalState(key, nextValue)]; | ||
}; | ||
|
||
return { | ||
setGlobalState, | ||
useGlobalState, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.