on master help sign in
refactor: break the actual hook out into its own local function
commit332575f1d8aba29d7bd0e8f67660bf931e6e7bab authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 11 Jun 2019, 12:43 (7 years ago) parentdc215db
1 file changed+15−7
src/bridge/store/history.js+15−7
src/bridge/store/history.js+15−7
@@ -6,7 +6,7 @@ import { ROUTE_NAMES } from '../lib/routeNames';
66
77export const HistoryContext = React.createContext(null);
88
9−export function HistoryProvider({ initialRoutes = [], children }) {
9+function _useHistory(initialRoutes = []) {
1010 const [history, setHistory] = useState(initialRoutes);
1111
1212 const value = {
@@ -43,11 +43,24 @@ export function HistoryProvider({ initialRoutes = [], children }) {
4343 // TODO: disposer functon
4444 });
4545
46+ return value;
47+}
48+
49+export function HistoryProvider({ initialRoutes, children }) {
50+ const history = _useHistory(initialRoutes);
51+
4652 return (
47− <HistoryContext.Provider value={value}>{children}</HistoryContext.Provider>
53+ <HistoryContext.Provider value={history}>
54+ {children}
55+ </HistoryContext.Provider>
4856 );
4957}
5058
59+// Hook version
60+export function useHistory() {
61+ return useContext(HistoryContext);
62+}
63+
5164// HOC version
5265export const withHistory = Component =>
5366 forwardRef((props, ref) => (
@@ -55,8 +68,3 @@ export const withHistory = Component =>
5568 {history => <Component ref={ref} history={history} {...props} />}
5669 </HistoryContext.Consumer>
5770 ));
58−
59−// Hook version
60−export function useHistory() {
61− return useContext(HistoryContext);
62−}