简体   繁体   English

我需要访问登录时最初存储在 redux 商店中的用户信息..我该怎么做? React-redux

[英]i need to access user info which are initially store in redux store when logged in.. How can i do that? React-redux

authReducer.js authReducer.js

import {
  LOGIN_SUCCESS,
  LOGIN_FAIL,
  REGISTER_SUCCESS,
  REGISTER_FAIL
} from '../Actions/types';


export default (state = { isLoading: true, user: [] }, action) => {
  switch(action.type){
    case LOGIN_SUCCESS:
      localStorage.setItem('token',action.payload.token);
      return {
        ...state,
        user: action.payload.data,
      }
      
    case LOGIN_FAIL:
      return state;
    case REGISTER_SUCCESS:
      return {
        ...state,
        user: action.payload.data,
      }
    case REGISTER_FAIL:
      return state;
    default:
      return state;
  }
}

profile.js配置文件.js

import React from 'react'
import {  useSelector} from 'react-redux'

export const EditProfile = () => {

   const data = useSelector((state) => state.user);
  return (
    <div>
      {data}
    </div>
  )
}

index.js (rootReducer) index.js (rootReducer)

import { combineReducers } from 'redux';
import authReducer from './authReducer';

export default combineReducers({
  auth: authReducer,
})

store.js商店.js

import {createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './Reducers';

const initialState = {};
const middleware = [thunk];

const store = createStore(rootReducer,initialState,compose(
    applyMiddleware(...middleware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()  
));

export default store;

So Here when i logged in the details were on user object so i need to access then in profile.js so that i can display the details of the user I don't how can i access i need to save the user data which are stored in user (redux store).所以在这里,当我登录时,详细信息位于用户对象上,因此我需要在 profile.js 中访问然后才能显示用户的详细信息 我不知道如何访问我需要保存存储的用户数据在用户(redux 存储)中。 I don't know what i am missing any help will be grateful我不知道我错过了什么任何帮助将不胜感激

I don't know how you setup your redux.我不知道你是如何设置你的redux的。

This is how I usually set them up:-这就是我通常设置它们的方式:-

A. Reducer A.减速机
  • /slices/auth.js
import { createSlice } from '@reduxjs/toolkit'

const initialState = {
  user: [],
  isLoading: false
}

export const authSlice = createSlice({
  name: 'auth',
  initialState,
  // // The `reducers` field lets us define reducers and generate associated actions
  reducers: {
    setUser: (state, action) => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.user = action.payload
    },
    setLoading: (state, action) => {
      state.loading = action.payload
    }
  }
})

export const { setUser, setLoading } = authSlice.actions;

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state: RootState) => state.counter.value)`
export const selectUser = (state) => state.auth.user
export const selectLoading = (state) => state.auth.isLoading

export default authSlice.reducer;

B. Store B. 商店

  • store.js
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'
import authReducer from '../slices/auth'

export const store = configureStore({
  reducer: {
    auth: authReducer
  },
  middleware: getDefaultMiddleware({
    serializableCheck: false
  })
})
C. App Component C. 应用组件
import { Provider } from 'react-redux'
import { store } from '../app/store'

export default function App() {
  return {
    <>
      <Provider store={store}>
        {/* your content... */}    
      </Provider>
    </>
  }
}
D. Component (where you use the redux) D. 组件(你使用 redux 的地方)
import { useContext } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { selectUser } from '../slices/auth'

export default function EditProfile() {
  const dispatch = useDispatch()
  const user = useSelector(selectUser)  

  return {
    <>
      {user}
    </>
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最初在``中分配子存储时,如何从子组件访问`react-redux`存储 <Provider> `?&gt; - How can I access `react-redux` store from child components when I initially assign it in `<Provider>`?> React-Redux:如何访问父组件中的商店? - React-Redux: How do I access the store in Parent Component? 当组件“连接”到react-redux存储时,我是否需要React.PureComponent? - Do I need React.PureComponent when the component is `connect`ed to react-redux store? React-redux 组件每次在商店中发生和未连接的 state 更改时都会重新渲染。 我该如何预防? - React-redux component rerenders every time when and unconnected state changes in the store . How do I prevent it? React-Redux:首次安装组件时如何获取数据 - React-Redux: how can i get store data when component first mounts 我可以将商店的状态与react-redux中的组件绑定吗? - Can I bind store's state with a component in react-redux? 在 react-redux 中,将所有内容(图像、配置文件信息、用户信息等)存储在 redux-persist 存储中? - In react-redux, store everything (images, profile info, user info, etc) in redux-persist store? 如何在 react-redux 中的 axios.get 方法中传递登录用户的 ID? - How can I pass the ID of logged-in user in the axios.get method in react-redux? 如何使用 React Redux 存储登录 state 的用户 - How to store user logged in state with React Redux 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM