宁波网站建设公司哪家好,SEO如何建设网站,怎样做网址有自己的模板,河北seo推广公司结论#xff1a;HashRouter不支持location.state
文档#xff1a;ReactRouter v5 从文档可看到history.push()方法支持2个参数#xff1a;path, [state] state即是location.state#xff0c;常用于隐式地传递状态参数 但文档未提的是#xff0c;仅适用于BrowserRouterHashRouter不支持location.state
文档ReactRouter v5 从文档可看到history.push()方法支持2个参数path, [state] state即是location.state常用于隐式地传递状态参数 但文档未提的是仅适用于BrowserRouterHashRouter下传入后无效通过location.state取不到值。
个人场景微前端适配两种路由模式这条路走不通亲测避坑。
Demo
import React, { useEffect } from react;
import { useHistory, useLocation, BrowserRouter, HashRouter } from react-router-dom-v5;const ComponentA () {const history useHistory()useEffect(() {history.push(xxx/ComponentB, { from: aaa})}, [])
}
const ComponentB () {const location useLocation()useEffect(() {console.log(location.state)// BrowserRouter: { from: aaa }// HashRouter: undefined}, [location])
}// 上层Router
BrowserRouterRoute pathxxx/ComponentA component{ComponentA} /Route pathxxx/ComponentB component{ComponentB} /
/BrowserRouterHashRouterRoute pathxxx/ComponentA component{ComponentA} /Route pathxxx/ComponentB component{ComponentB} /
/HashRouter