简体   繁体   English

反应:行动必须是简单的 Object

[英]React : Action Must Be Plain Object

I have below project structure -我有以下项目结构 -

在此处输入图像描述

I have GlobalStore.js where I have below code:-我有 GlobalStore.js 我有以下代码: -

import React from 'react'

const GlobalContext=React.createContext();
const GlobalProvider=GlobalContext.Provider;
const GlobalConsumer=GlobalContext.Consumer;


export  {GlobalProvider,GlobalConsumer}

LoginReducers/Login_Action.js with below code - LoginReducers/Login_Action.js 带有以下代码 -

const VERIFY_CREDENTIALS ='VERIFY_CREDENTIALS'
export function VerifyCredentials()
{
    return{
        type :VERIFY_CREDENTIALS
    }
}

LoginReducers/Login_Reducers.js with below code - LoginReducers/Login_Reducers.js 使用以下代码 -

import Axios from "axios";
import { VerifyCredentials } from "./Login_Action";

const initialState={
    userName:"",
    password:"",
    isVarified:false
}
const url='http://localhost:52016/api/values/';
export const  LoginReducer=(state=initialState,action)=>{
    switch (action.type) {
        case 'VERIFY_CREDENTIALS':
            
            Axios.get(url)
                 .then(x=>{
                     alert(x.data);

                 })
    
        default:
            break;
    }
}

GlobalStorage/store.js with below code -具有以下代码的 GlobalStorage/store.js -

import { createStore } from 'redux';
import { LoginReducer } from "../Components/LoginReducers/Login_Reducers";

export const  store=createStore(LoginReducer);

App.js with below code -带有以下代码的 App.js -

import logo from './logo.svg';
import './App.css';
import Login from './Components/Login';
import { store } from "./GlobalStorage/store";
import   {GlobalProvider,GlobalConsumer} from "./GlobalStore";
function App() {
  return (
    <div className="App">
      <GlobalProvider value={store}> 
     <Login></Login>
     </GlobalProvider>
    </div>
  );
}

export default App;

I am getting below error:-我收到以下错误:-

在此处输入图像描述

Please suggest what changes can be made in order to resolve this error?请建议可以进行哪些更改以解决此错误?

Also please suggest if above code structure is recommended or not where I am sharing store through GlobalProvider .另外请建议是否推荐以上代码结构,我通过GlobalProvider共享store

I installed -我安装了 -

npm install --save redux-thunk

Also added below code in store.js -还在 store.js 中添加了以下代码 -

import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

export const  store=createStore(LoginReducer,applyMiddleware(thunk));

Url which helped me - Url 帮助了我-

https://www.npmjs.com/package/redux-thunk https://www.npmjs.com/package/redux-thunk

Credit For Solution -信用解决方案 -

Nilesh Patel's Comment尼莱什·帕特尔的评论

Also your reducer should be pure function and do your staff on action like this:此外,您的减速器应该是纯 function 并让您的员工采取如下行动:

Action行动

import Axios from "axios";

const VERIFY_CREDENTIALS ='VERIFY_CREDENTIALS';
const ERROR_CREDENTIALS='ERROR_CREDENTIALS';

export function VerifyCredentials(username,password)
{
  return Axios.post(url,{username,password})
    .then(x=>{
        {type :VERIFY_CREDENTIALS,payload:{userData:x.data, isVarified:true} }
    })
    .catch((err) => {type :ERROR_CREDENTIALS});
}

Reducer减速器

import { VERIFY_CREDENTIALS ,ERROR_CREDENTIALS} from "./Login_Action";

const initialState={
    userData:"",
    isVarified:false
}
const url='http://localhost:52016/api/values/';
export const  LoginReducer=(state=initialState,action)=>{
    switch (action.type) {
        case VERIFY_CREDENTIALS:
            return action.payload;
        case ERROR_CREDENTIALS:
            return state;
        default:
            return state;
    }
}

You are very wrong in your code !!!您的代码非常错误!

Please note all of the following.请注意以下所有事项。 Please also read the documentation - for write better code另请阅读文档 - 编写更好的代码

use react-redux library - and for create async actions use redux-thunk使用react-redux库 - 创建异步操作使用redux-thunk

import   {Provider} from "react-redux"; // you need this
import logo from './logo.svg';
import './App.css';
import Login from './Components/Login';
import { store } from "./GlobalStorage/store";

function App() {
  return (
    <div className="App">
      <Provider store={store}> 
         <Login/>
      </Provider>
    </div>
  );
}

export default App;

Login_Reducers.js Login_Reducers.js

const initialState={
    userName:"",
    password:"",
    isVarified:false,
    loading:false,
    error:false,
}
export const  LoginReducer=(state=initialState,action)=>{
    switch (action.type) {
        case 'VERIFY_CREDENTIALS':
            // return new state 
            alert(action.payload)
            return {
              ...state,
              username:"John",
              password:'12345678'
            }
        case "VERIFY_CREDENTIALS_LOADING":
            return{
               ...state,
               loading:true,
            }
        case "VERIFY_CREDENTIALS_ERROR":
            return{
               ...state,
               error:true,
            }
        default:
            return state
    }
}

Login_Action.js Login_Action.js

const VerifyCredentialsError = ()=>({type:"VERIFY_CREDENTIALS_ERROR"})
const VerifyCredentialsLoading = ()=>({type:"VERIFY_CREDENTIALS_LOADING"})
const VerifyCredentialsSuccess = (data)=>({type:"VERIFY_CREDENTIALS",payload:data})
export const RequestVerifyCredentials = ()=>{
    return(dispatch)=>{
       dispatch(VerifyCredentialsLoading())
       Axios.get(url)
           .then(x=>{
             alert(x.data);
             dispatch(VerifyCredentialsSuccess(x.data))
            })
            .catch(err => dispatch(VerifyCredentialsError()))
    }
}

Login.js登录.js

import {useEffect} from 'react'
import {useDispatch} from 'react-redux'
import {RequestVerifyCredentials} from 'Login_Action.js'
const Login = ()=>{
const dispatch = useDispatch()

useEffect(()=>{
  dispatch(RequestVerifyCredentials())
},[dispatch])

return(
   <h2>Login Component</h2>
)

} }

react-redux Provider DOCS: react-redux提供者文档:

https://react-redux.js.org/api/provider https://react-redux.js.org/api/provider

react-redux Hook DOCS: react-redux挂钩文档:

https://react-redux.js.org/api/hooks https://react-redux.js.org/api/hooks

redux-thunk github repository and docs: redux-thunk github 存储库和文档:

https://github.com/reduxjs/redux-thunk https://github.com/reduxjs/redux-thunk

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM