on master help sign in
refactor: break the actual hook out into its own local function
332575fMatt Condon (shrugs)
src/bridge/store/history.js+15−7
@@ -6,7 +6,7 @@ import { ROUTE_NAMES } from '../lib/routeNames';
6
6
7export const HistoryContext = React.createContext(null);
7export const HistoryContext = React.createContext(null);
8
8
9−export function HistoryProvider({ initialRoutes = [], children }) {
9+function _useHistory(initialRoutes = []) {
10 const [history, setHistory] = useState(initialRoutes);
10 const [history, setHistory] = useState(initialRoutes);
11
11
12 const value = {
12 const value = {
@@ -43,11 +43,24 @@ export function HistoryProvider({ initialRoutes = [], children }) {
43 // TODO: disposer functon
43 // TODO: disposer functon
44 });
44 });
45
45
46+ return value;
47+}
48+
49+export function HistoryProvider({ initialRoutes, children }) {
50+ const history = _useHistory(initialRoutes);
51+
46 return (
52 return (
47− <HistoryContext.Provider value={value}>{children}</HistoryContext.Provider>
53+ <HistoryContext.Provider value={history}>
54+ {children}
55+ </HistoryContext.Provider>
48 );
56 );
49}
57}
50
58
59+// Hook version
60+export function useHistory() {
61+ return useContext(HistoryContext);
62+}
63+
51// HOC version
64// HOC version
52export const withHistory = Component =>
65export const withHistory = Component =>
53 forwardRef((props, ref) => (
66 forwardRef((props, ref) => (
@@ -55,8 +68,3 @@ export const withHistory = Component =>
55 {history => <Component ref={ref} history={history} {...props} />}
68 {history => <Component ref={ref} history={history} {...props} />}
56 </HistoryContext.Consumer>
69 </HistoryContext.Consumer>
57 ));
70 ));
58−
59−// Hook version
60−export function useHistory() {
61− return useContext(HistoryContext);
62−}