【问题标题】:How to save current user data using react-redux?如何使用 react-redux 保存当前用户数据?
【发布时间】:2021-08-16 21:42:43
【问题描述】:

我创建了简单的 MERN 应用。我使用护照进行身份验证。

我有 home、login、register secret 和 submit 页面有.只有登录的用户可以看到 secret 、 home 和 submit 页面,只有未登录的用户可以看到 login 和 注册页面。我使用 axios 后端发送数据。用户登录,注册成功,但登录后用户多次获取用户数据。我不知道如何解决这个问题。我的后端没有错误。它运作良好。


-------------- 秘密页面--------------
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Redirect } from "react-router";
import { user_ } from "../actions/register_action";

const Secret =  ()=>{
    const dispatch = useDispatch();

    const user = useSelector( state => state.user );
    
    useEffect( ()=>{
       dispatch( user_ );
    },[] );
    
    console.log(user);

    if(user === null) return <Redirect to = "/login" />
    else 
    return (
        <div className="jumbotron text-center">
            <div className="container">
                <i className="fas fa-key fa-6x"></i>
                <h1 className="display-3">You've Discovered My Secret!</h1>
                <p className="secret-text">Jack Bauer is my hero.</p>
                <hr />
        
                <a className="btn btn-light btn-lg" href="/logout" role="button">Log Out</a>
                <a className="btn btn-dark btn-lg" href="/submit" role="button">Submit a Secret</a>
            </div>
        </div>

    );
} 
export default Secret;

------------- 动作 --------------

import { fail, user, USER_LOGIN_FAIL, USER_LOGIN_REQUEST, USER_LOGIN_SUCCESS,
     USER_REGISTER_FAIL, USER_REGISTER_REQUEST, USER_REGISTER_SUCCESS
     } from "../constants/all";
import axios from "axios";


const user_Register = (email, password) => async (dispatch) =>{
    dispatch({ type: USER_REGISTER_REQUEST });
    try {
        const {data} = await axios( {
            method: "POST",
            data: {
                "username": email,
                "password": password
            },
            withCredentials: true,
            url: "http://localhost:3001/register"
        });  
        
        dispatch({ type: USER_REGISTER_SUCCESS, payload: data });

    } catch (error) {
        dispatch({type: USER_REGISTER_FAIL, error: error.message});
    }
}

const user_Login = (email, password) => async (dispatch) =>{
    dispatch({ type: USER_LOGIN_REQUEST });
    try {
        const {data} = await axios( {
            method: "POST",
            data: {
                username: email,
                password: password
            },
            withCredentials: true,
            url: "http://localhost:3001/login"
        });  
        dispatch({ type: USER_LOGIN_SUCCESS, payload: data });
        
    } catch (error) {
        dispatch({type: USER_LOGIN_FAIL, error: error.message});
    }
}

const user_ = async (dispatch) => {
    try{
        const {data} = await axios({
            method: "GET",
            withCredentials: true,
            url: 'http://localhost:3001'
        });
        if(data.isAuth){
            dispatch({type:user, payload:data});
        }
        else{
            dispatch({type: fail});
        }
    }
    catch (error){

    }
}

export {user_Register, user_Login, user_};

谁有更好的主意? 非常感谢!

【问题讨论】:

    标签: reactjs redux react-redux axios redux-thunk


    【解决方案1】:

    您能否尝试将您的 user_ 操作创建器重写为返回函数的函数:

    const user_ = () => async (dispatch) => {
      try {
        const { data } = await axios({
          method: "GET",
          withCredentials: true,
          url: "http://localhost:3001",
        });
        if (data.isAuth) {
          dispatch({ type: user, payload: data });
        } else {
          dispatch({ type: fail });
        }
      } catch (error) {}
    };
    

    然后在您的Secret 组件的useEffect 中,调用user_ 动作创建者来创建动作:

    useEffect(() => {
      dispatch(user_());
    }, []);
    

    我不肯定这是问题所在,但它似乎确实应该以任何一种方式完成。

    【讨论】:

    • 我以另一种方式编写 _user 操作,但仍然采用一种方式。登录后我进入秘密页面,但用户数据最初为空,页面重定向登录。获取用户数据后。我需要第一次获取当前用户。
    • 为什么需要退货?我看到一些 youtube 视频会获取这样的用户信息。
    • 我认为这个问题是因为您在获取数据之前尝试选择数据,所以它是null。而且您正在从页面重定向。我认为要解决这个问题,您将不得不重新构建一些逻辑,以便当您选择用户时,您已经尝试获取它。
    • 我在哪里可以找到关于这个的好教程?在 mail.ru 中,当我在我的页面中向我发送消息时,我看到了那个时间。它做redux。我该怎么做?
    • 我不知道。如果我遇到什么事情,我会在另一条评论中发布并标记你。很抱歉,我无法提供更多帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多