簡體   English   中英

無法將 redux 渲染到其他組件 reactjs

[英]Can't render redux to other component reactjs

我正在嘗試在我當前的項目中使用 redux。 但是,我不能將調度數據用於另一個組件。

這是我的代碼:

驗證.js

import { useDispatch, useSelector } from 'react-redux';
import { setLoginDetails } from 'redux/actions/loginActions';

function Authenticate() {
  const [data, setData] = useState([]);
  const dispatch = useDispatch();
  const loginDetails = useSelector((state) => state);
  
  const validateLogin = async() => {
    const response = await.get('/api')
    let responseValue = response.data.success
    if(responseValue === true) {
      let responseResp = JSON.parse(response.data.resp)
      dispatch(setLoginDetails(responseResp.data.user))
      /** here's the console logs
   console.log('responseResp',responseResp)
console.log('AuthenticationOAK/responseResp.data.',responseResp.data)
console.log('AuthenticationOAK/responseResp.data.user',responseResp.data.user)
      */
      window.location = '/home'
    }
    else{
      //error 
    }
  }
  useEffect(()=>{
    validateLogin();
  },[])
return(
  <div>Authenticating....</div>
)
}
export default Authenticate;

loginAction.js

import { ActionTypes } from '../constants/action-types';
export const setLoginDetails = (loginDetails) => {
  return {
    type: ActionTypes.SET_LOGIN,
    payload: loginDetails,
  }
}

動作類型.js

export const ActionTypes = {
  SET_LOGIN: 'SET_LOGIN',
}

loginReducer.js

import { ActionTypes } from '../constants/action-types';
const initialState = {
  loginDetails: [],
}
export const loginReducer = (state = initialState, {type, payload}) => {
  console.log('loginReducer/state', state)
  console.log('loginReducer/payload',payload)
  console.log('loginReducer/type', type)
  console.log('loginReducer/initialState',initialState)
  switch(type){
    case ActionTypes.SET_LOGIN:
      return {...state, loginDetails: payload}
    default:
      return state;
  }
}

主頁.js

import { useSelector } from 'react-redux';

function Home(){
  const loginDetails = useSelector((state) => state.allLoginDetails.loginDetails)
  const { email, userName, firstName } = loginDetails;
  return(
    <h1>username {userName}</h1>
  )
  
}
export default Home;

index.js /redux/reducers/index.js

import { combineReducers } from 'redux'
import { loginReducer } from './loginReducer'
const reducers = combineReducers({
  allLoginDetails: loginReducer,
});
export default reducers;

store.js

import { createStore } from 'redux'
import reducers from './index'
const store = createStore(
  reducers,
  {},
);
export dafault store;

索引.js

import React from 'react'
import { render } from 'react-dom'
import App from './App'
import {createStore} from 'redux'
import { Provider } from 'react-redux'
import store from '../src/redux/reducers/store'
render(
  <Provider store={store}>
    <App />
  </Provider>,
document.getElementById('root'));

下面是來自Authenticate.js的控制台日志的結果在此處輸入圖像描述

loginReducer.js的控制台日志截圖

在此處輸入圖像描述

希望你能幫助我解決這個問題,我很難做到這一點。 嘗試在initialState上使用硬編碼值並且它正在正確呈現。 但是當我使用動態數據時,它不會呈現。 先感謝您。 注意安全!

在這個文件中:Authenticate.js

useEffect(()=>{
    Authenticate(); // it must be validateLogin()?
  },[])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM