react Route v4 中是否有 onChange 的替换参数
Is there an replacement Parameter for onChange in react Route v4
typescript -React Js:我尝试使用 onChange Propriete,但出现以下问题:没有与此调用匹配的重载。
<HashRouter>
<Switch>
<Route
path="/"
**onChange={() => {**
if (!authstore.isLoggedIn && (location as any).pathName !== "/login") {
authstore.checkLogin();
}
}}
>
<div>Hallo.</div>
<Route exact path="/" component={() => <Index />} />
<Route path="/login" component={() => <Login />} />
<Route path="/deployment/deploymentprojects" component={() => <DeploymentProjectsPageflow />} />
<Route path="/deployment/deploymenttypes" component={() => <DeploymentTypesPageflow />} />
<Route path="/deployment/sonarqube/qualitygates" component={() => <QualityGatePageflow />} />
<Route path="/deployment/sonarqube/qualityprofiles" component={() => <QualityProfilePageflow />} />
<Route path="/deployment/sonarqube/projecttags" component={() => <ProjectTagPageflow />} />
<Route path="/deployment/dependencies" component={() => <DependenciesTable />} />
<Route path="/deployment/rollout/release" component={() => <Release />} />
<Route path="/deployment/rollout/patch" component={() => <Patch />} />
<Route path="/antd" component={() => <AntdLink />} />
<Route path="/kafka" component={() => <KafkaView />} />
<Route path="/pageFlow" component={() => <MyPageflow />} />
<Route
path="/antd/components/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
<Route path="/antd/:component" component={(parameter: any) => <AntRouter params={parameter} />}></Route>
</Route>
</Switch>
</HashRouter>
请帮我在 React Router v4 中找到 onChange 参数的替代解决方案
您可以将 useLocation
与 useEffect
结合使用:
https://reactrouter.com/web/api/Hooks/uselocation
https://reactjs.org/docs/hooks-effect.html
import React, { useEffect } from "react";
import { HashRouter, Switch, Route, useLocation } from "react-router-dom";
const YourWrapper = () => {
const location = useLocation();
useEffect(() => {
if (!authstore.isLoggedIn && location.pathname !== "/login") {
authstore.checkLogin();
}
}, [location]);
return (
<HashRouter>
<Switch>
<div>Hallo.</div>
<Route exact path="/" component={() => <Index />} />
<Route path="/login" component={() => <Login />} />
<Route
path="/deployment/deploymentprojects"
component={() => <DeploymentProjectsPageflow />}
/>
<Route
path="/deployment/deploymenttypes"
component={() => <DeploymentTypesPageflow />}
/>
<Route
path="/deployment/sonarqube/qualitygates"
component={() => <QualityGatePageflow />}
/>
<Route
path="/deployment/sonarqube/qualityprofiles"
component={() => <QualityProfilePageflow />}
/>
<Route
path="/deployment/sonarqube/projecttags"
component={() => <ProjectTagPageflow />}
/>
<Route
path="/deployment/dependencies"
component={() => <DependenciesTable />}
/>
<Route
path="/deployment/rollout/release"
component={() => <Release />}
/>
<Route path="/deployment/rollout/patch" component={() => <Patch />} />
<Route path="/antd" component={() => <AntdLink />} />
<Route path="/kafka" component={() => <KafkaView />} />
<Route path="/pageFlow" component={() => <MyPageflow />} />
<Route
path="/antd/components/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
<Route
path="/antd/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
</Switch>
</HashRouter>
);
};
typescript -React Js:我尝试使用 onChange Propriete,但出现以下问题:没有与此调用匹配的重载。
<HashRouter>
<Switch>
<Route
path="/"
**onChange={() => {**
if (!authstore.isLoggedIn && (location as any).pathName !== "/login") {
authstore.checkLogin();
}
}}
>
<div>Hallo.</div>
<Route exact path="/" component={() => <Index />} />
<Route path="/login" component={() => <Login />} />
<Route path="/deployment/deploymentprojects" component={() => <DeploymentProjectsPageflow />} />
<Route path="/deployment/deploymenttypes" component={() => <DeploymentTypesPageflow />} />
<Route path="/deployment/sonarqube/qualitygates" component={() => <QualityGatePageflow />} />
<Route path="/deployment/sonarqube/qualityprofiles" component={() => <QualityProfilePageflow />} />
<Route path="/deployment/sonarqube/projecttags" component={() => <ProjectTagPageflow />} />
<Route path="/deployment/dependencies" component={() => <DependenciesTable />} />
<Route path="/deployment/rollout/release" component={() => <Release />} />
<Route path="/deployment/rollout/patch" component={() => <Patch />} />
<Route path="/antd" component={() => <AntdLink />} />
<Route path="/kafka" component={() => <KafkaView />} />
<Route path="/pageFlow" component={() => <MyPageflow />} />
<Route
path="/antd/components/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
<Route path="/antd/:component" component={(parameter: any) => <AntRouter params={parameter} />}></Route>
</Route>
</Switch>
</HashRouter>
请帮我在 React Router v4 中找到 onChange 参数的替代解决方案
您可以将 useLocation
与 useEffect
结合使用:
https://reactrouter.com/web/api/Hooks/uselocation
https://reactjs.org/docs/hooks-effect.html
import React, { useEffect } from "react";
import { HashRouter, Switch, Route, useLocation } from "react-router-dom";
const YourWrapper = () => {
const location = useLocation();
useEffect(() => {
if (!authstore.isLoggedIn && location.pathname !== "/login") {
authstore.checkLogin();
}
}, [location]);
return (
<HashRouter>
<Switch>
<div>Hallo.</div>
<Route exact path="/" component={() => <Index />} />
<Route path="/login" component={() => <Login />} />
<Route
path="/deployment/deploymentprojects"
component={() => <DeploymentProjectsPageflow />}
/>
<Route
path="/deployment/deploymenttypes"
component={() => <DeploymentTypesPageflow />}
/>
<Route
path="/deployment/sonarqube/qualitygates"
component={() => <QualityGatePageflow />}
/>
<Route
path="/deployment/sonarqube/qualityprofiles"
component={() => <QualityProfilePageflow />}
/>
<Route
path="/deployment/sonarqube/projecttags"
component={() => <ProjectTagPageflow />}
/>
<Route
path="/deployment/dependencies"
component={() => <DependenciesTable />}
/>
<Route
path="/deployment/rollout/release"
component={() => <Release />}
/>
<Route path="/deployment/rollout/patch" component={() => <Patch />} />
<Route path="/antd" component={() => <AntdLink />} />
<Route path="/kafka" component={() => <KafkaView />} />
<Route path="/pageFlow" component={() => <MyPageflow />} />
<Route
path="/antd/components/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
<Route
path="/antd/:component"
component={(parameter: any) => <AntRouter params={parameter} />}
></Route>
</Switch>
</HashRouter>
);
};