从历史NPM URL变化的CreateBrowserHistory,而不是组件React-Redux
具有 React 和 React-dom 的相同版本 “反应”:“^18.2.0”, “ react-dom”:“^18.2.0”,历史是最新的 “历史”:“^5.3.0”,
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import SignInSide from "./components/mui/signIn/SignInSide";
import store from "./Store/Store";
import { Provider } from "react-redux";
import Dashboard from "./components/mui/Dashboard";
import history from './utils/history';
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Provider store={store}>
<Router history={history}>
<Routes>
<Route path="/login" element={<SignInSide />} />
<Route path="/home" element={<Dashboard />} >
</Route>
<Route path="/" element={<App />} />
</Routes>
</Router>
</Provider>
</React.StrictMode>
);
function SignInSide(props) {
const handleSubmit = (event) => {
event.preventDefault();
const form = new FormData(event.currentTarget);
let user = {
email: form.get('email'),
password: form.get('password')
}
console.log(user);
props.signIn(user);
};
return (....);
从singin button调用handlesubmit
import { createBrowserHistory } from "history";
export function LoginUser(LogInData) {
let navigate = useNavigate;
return (dispatch) => {
dispatch(AuthActions.userSignIn());
signInWithEmailAndPassword(auth, LogInData.email, LogInData.password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
dispatch(AuthActions.userSignInSuccess(user));
setPersistence(auth, browserSessionPersistence)
.then(() => {
return signInWithEmailAndPassword(
auth,
LogInData.email,
LogInData.password
);
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
});
history.push("/home");
// console.log(store);
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
dispatch(AuthActions.userSignInFailed(error));
});
};
}
使用history.push(“/home”)此仅替换了不替换组件加载的URL,因此请使用最新的NPM版本(如果有)或尽快建议使用Ready模板。
。
Having same version for react and react-dom
"react": "^18.2.0",
"react-dom": "^18.2.0" and history is latest
"history": "^5.3.0",
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import SignInSide from "./components/mui/signIn/SignInSide";
import store from "./Store/Store";
import { Provider } from "react-redux";
import Dashboard from "./components/mui/Dashboard";
import history from './utils/history';
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Provider store={store}>
<Router history={history}>
<Routes>
<Route path="/login" element={<SignInSide />} />
<Route path="/home" element={<Dashboard />} >
</Route>
<Route path="/" element={<App />} />
</Routes>
</Router>
</Provider>
</React.StrictMode>
);
function SignInSide(props) {
const handleSubmit = (event) => {
event.preventDefault();
const form = new FormData(event.currentTarget);
let user = {
email: form.get('email'),
password: form.get('password')
}
console.log(user);
props.signIn(user);
};
return (....);
Calling handleSubmit from singIn button
import { createBrowserHistory } from "history";
export function LoginUser(LogInData) {
let navigate = useNavigate;
return (dispatch) => {
dispatch(AuthActions.userSignIn());
signInWithEmailAndPassword(auth, LogInData.email, LogInData.password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
dispatch(AuthActions.userSignInSuccess(user));
setPersistence(auth, browserSessionPersistence)
.then(() => {
return signInWithEmailAndPassword(
auth,
LogInData.email,
LogInData.password
);
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
});
history.push("/home");
// console.log(store);
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
dispatch(AuthActions.userSignInFailed(error));
});
};
}
Using history.push("/home") this only Url replaced not component loading so please provide the solution with using latest npm version(if any) or suggest ready template ASAP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
React-Router-dom@6
browserrouter
不采用历史记录
prop。browserrouter
如果要使用自定义
历史
对象,则应导入“历史记录” 并传递历史
prop for您为应用程序创建的历史记录对象类型。历史记者
确保您的整个应用程序正在导入并引用传递给路由器的相同单个
历史>对象实例。
创建历史记录对象
导入
historyRouter
和自定义历史
对象。...
The
react-router-dom@6
BrowserRouter
doesn't take ahistory
prop.BrowserRouter
If you want to use a custom
history
object then you should import the HistoryRouter and pass thehistory
prop for the type of history object you are creating for your app.HistoryRouter
Ensure that your entire app is importing and referencing the same single
history
object instance that is passed to the router.Create the history object
Import the
HistoryRouter
and the customhistory
object....