0

当我注册时,它会将我带到仪表板页面,那里有一个带有用户名的欢迎说明,但问题是在我刷新或再次登录之前我看不到用户名。我正在共享我的注册 action.js 代码和我的仪表板

我的registration.js代码

const Register = (props) => {
const { classes } = props
const [name, setName] = useState('')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const dispatch = useDispatch();

const onRegister = async () => {
    dispatch(startRegister(name, email, password));
    props.history.push('/dashboard')
}

动作.js

export const startRegister = (name, email, password) => {
    // const { name, email, password } = props
    return () => {
        return firebase.auth().createUserWithEmailAndPassword(email, password)
            .then((currentUser) => {
                // Signed in
                const user = firebase.auth().currentUser;
                user.updateProfile({
                    displayName: name,
                }).then(function () {
                    console.log('Updated');

                    // Update successful.
                }).catch(function (error) {
                    // An error happened.
                    console.log(error);
                });

                // history.push('/dashboard')
                // ...
            })
            .catch((error) => {
                var errorMessage = error.message;
                console.log(errorMessage);
                // ..
            });
    };
};

仪表板.js

const Dashboard = props => {
const { classes } = props
const onSignout = async (e) => {
    await firebase.auth().signOut()
    //props.history.push('/')
}
const username = useSelector(state => state.auth.name);

return (
    <main className={classes.main}>
        <Paper className={classes.paper}>
            <Typography component="h1" variant="h5">
                {`Welcome User ${username ? username : ''}`}
4

1 回答 1

0

乍一看,我相信这个问题是因为 javascript 承诺,firebase.auth().createUserWithEmailAndPassword 是一个异步函数。

尝试这个

registration.js

const onRegister = async () => {
    const userData = {name, email, password}
    dispatch(startRegister(props, userData));
}

action.js中

export const startRegister = (props, userData) => {
    const { name, email, password } = userData;
    return () => {
        return firebase.auth().createUserWithEmailAndPassword(email, password)
            .then((currentUser) => {
                // Signed in
                const user = firebase.auth().currentUser;
                user.updateProfile({
                    displayName: name,
                }).then(function () {
                    console.log('Updated');

                    // Navigate to dashboard after data is been saved
                    props.history.push('/dashboard');

                }).catch(function (error) {
                    // An error happened.
                    console.log(error);
                });
            })
            .catch((error) => {
                var errorMessage = error.message;
                console.log(errorMessage);
                // ..
            });
        };
     };
于 2021-05-02T01:44:44.570 回答