Last active
November 27, 2019 10:56
-
-
Save fnky/7c96c7fa3ec0e36796675f83c6635e48 to your computer and use it in GitHub Desktop.
History provider with useState / useRef
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
import React from 'react'; | |
import { createBrowserHistory } from 'history'; | |
export const historyContext = React.createContext(null); | |
const HistoryContextProvider = historyContext.Provider; | |
// This is initialized at the time the file/module is imported, and not on component mount. | |
const history = createBrowserHistory(); | |
export function History(props) { | |
return <HistoryContextProvider value={history} {...props} />; | |
} |
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
import React from 'react'; | |
import { createBrowserHistory } from 'history'; | |
export const historyContext = React.createContext(null); | |
const HistoryContextProvider = historyContext.Provider; | |
export function History(props) { | |
// Initialize the instance in useState. Initialized on mount. Available from first render. | |
const [history] = React.useState(() => createBrowserHistory()); | |
return <HistoryContextProvider value={history} {...props} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment