到达路由器:在功能点击时重定向到 urI
Reach Router: Redirect to urI on function click
我目前正在努力解决如何在用户单击将注册帐户的按钮时重定向到主页的问题:
我目前的设置是这样的
function Application() {
const user = useContext(UserContext);
return (
user ?
<Router>
<LandingScreen path="landingscreen"/>
</Router>
:
<Router>
<SignUp path="signUp" />
<SignIn path="/" />
</Router>
);
}
export default Application;
const SignIn = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password).catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
});
};
我如何添加函数 signInWithEmailAndPasswordHandler 以便在调用它时将用户重定向到 /landingscreen?我已经阅读了 reach 文档,但我是 JSX 的新手并且正在努力实现它。
import { useHistory } from 'react-router-dom';
const SignIn = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const history = useHistory(); // Add this from redux-form
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password).catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
})
// Add this (if you want it call only if not catched error, place it before .catch
.then(() => {
history.push('/landingscreen');
});
};
您可以使用 react-router-dom
钩子并调用 history.push('url')
import { useHistory } from "react-router-dom";
const SignIn = () => {
const history = useHistory()
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password)
.then(() => {
// Logged in successfully
history.push('/landingscreen');
})
.catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
})
};
我目前正在努力解决如何在用户单击将注册帐户的按钮时重定向到主页的问题: 我目前的设置是这样的
function Application() {
const user = useContext(UserContext);
return (
user ?
<Router>
<LandingScreen path="landingscreen"/>
</Router>
:
<Router>
<SignUp path="signUp" />
<SignIn path="/" />
</Router>
);
}
export default Application;
const SignIn = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password).catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
});
};
我如何添加函数 signInWithEmailAndPasswordHandler 以便在调用它时将用户重定向到 /landingscreen?我已经阅读了 reach 文档,但我是 JSX 的新手并且正在努力实现它。
import { useHistory } from 'react-router-dom';
const SignIn = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const history = useHistory(); // Add this from redux-form
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password).catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
})
// Add this (if you want it call only if not catched error, place it before .catch
.then(() => {
history.push('/landingscreen');
});
};
您可以使用 react-router-dom
钩子并调用 history.push('url')
import { useHistory } from "react-router-dom";
const SignIn = () => {
const history = useHistory()
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const signInWithEmailAndPasswordHandler = (event,email, password) => {
event.preventDefault();
auth.signInWithEmailAndPassword(email, password)
.then(() => {
// Logged in successfully
history.push('/landingscreen');
})
.catch(error => {
setError("Error signing in with password and email!");
console.error("Error signing in with password and email", error);
})
};