如何在点击处理程序中使用 react router 1.0.x 进行客户端路由?
How to do Client side routing using react router 1.0.x in a click handler?
我有一个单页应用程序。我可以使用 react-router 提供的 <Link>
标签来使用客户端路由器。
<Link to='/nextRoute'>next</Link
但是,如果我需要从点击处理程序更改路由怎么办? (在离开之前我还有一些事情要做)
使用 window.location.assign
将导致整个内容再次从服务器获取,但我需要使用客户端路由器。
我相信 1.0.x 使用了 history 包。
您必须检查您使用的是哪个版本的历史记录,但文档中有:
// Push a new entry onto the history stack.
history.push('/home')
// Replace the current entry on the history stack.
history.replace('/profile')
// Push a new entry with state onto the history stack.
history.push({
pathname: '/about',
search: '?the=search',
state: { some: 'state' }
})
// Change just the search on an existing location.
history.push({ ...location, search: '?the=other+search' })
// Go back to the previous history entry. The following
// two lines are synonymous.
history.go(-1)
history.goBack()
我有一个单页应用程序。我可以使用 react-router 提供的 <Link>
标签来使用客户端路由器。
<Link to='/nextRoute'>next</Link
但是,如果我需要从点击处理程序更改路由怎么办? (在离开之前我还有一些事情要做)
使用 window.location.assign
将导致整个内容再次从服务器获取,但我需要使用客户端路由器。
我相信 1.0.x 使用了 history 包。
您必须检查您使用的是哪个版本的历史记录,但文档中有:
// Push a new entry onto the history stack.
history.push('/home')
// Replace the current entry on the history stack.
history.replace('/profile')
// Push a new entry with state onto the history stack.
history.push({
pathname: '/about',
search: '?the=search',
state: { some: 'state' }
})
// Change just the search on an existing location.
history.push({ ...location, search: '?the=other+search' })
// Go back to the previous history entry. The following
// two lines are synonymous.
history.go(-1)
history.goBack()