history icon indicating copy to clipboard operation
history copied to clipboard

typing v5: Provide state type when creating history

Open CPatchane opened this issue 4 years ago • 1 comments

👋

When creating an history using createBrowserHistory or createMemoryHistory, it could be great to be able to pass a type that will be pass as state instead of always having object | null.

Here is what it could look like:

const history = createBrowserHistory<myHistoryStateType>()
const history = createMemoryHistory<myHistoryStateType>()

It will allow to do something like:

type myHistoryStateType = {
  extra: string
}
const history = createBrowserHistory<myHistoryStateType>()

history.listen(({ location }) => {
  const { state } = location
  console.log(state.extra)
})

instead of currently have to force the type after:

type myHistoryStateType = {
  extra: string
}
const history = createBrowserHistory()

history.listen(({ location }) => {
  const state = location.state as HistoryStateType
  console.log(state.extra)
})

CPatchane avatar Oct 26 '20 10:10 CPatchane

This would make the workaround for #791 slightly less hackish. Currently I guess I'm gonna have to replace the initial location to attach state to mark the very first entry to prevent going back beyond it

jedwards1211 avatar Nov 20 '20 02:11 jedwards1211