当redux状态改变时,可以通过使用react-router-dom库中的useHistory
钩子函数来实现导航。
首先,确保已经安装了react-router-dom
库:
npm install react-router-dom
然后,在需要导航的组件中,导入useHistory
:
import { useHistory } from 'react-router-dom';
接下来,在组件中使用useHistory
钩子函数获取history
对象:
const history = useHistory();
当redux状态发生改变时,可以在相应的地方调用history.push
方法进行导航,例如:
// 当redux状态改变时的处理逻辑
function handleReduxStateChange() {
// 导航到指定路由
history.push('/new-route');
}
这样,当redux状态改变时,就会自动导航到指定的路由/new-route
。
关于useHistory
的更多信息和用法,可以参考腾讯云的React Router文档:
React Router - useHistory
领取专属 10元无门槛券
手把手带您无忧上云