【问题标题】:Redux Action not hitting Reducer [React]Redux Action 没有击中 Reducer [React]
【发布时间】:2019-05-16 15:36:18
【问题描述】:

正如标题所说,我的 React 应用程序出现问题,我可以点击 Redux Action,但之后它没有点击 Reducer。我查看了我过去从事的一个项目,以及这里的几篇文章,但我不确定我的代码有什么问题阻止 Action 击中减速器。我已经粘贴了下面的代码,但如果还有什么我可以提供的,请告诉我。

index.js:

import React from 'react';
import ReactDom from 'react-dom';
import App from './components/App.jsx';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducer from './reducers/usersRedcuers';
import './index.css';

const store = createStore(reducer);

ReactDom.render(
    <Provider store={store}>
      <App />
    </Provider>, document.getElementById('root')
)

App.jsx 组件:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import Auth from '../modules/Auth';
import { login } from '../actions/usersActions';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      auth: null,
      token: '',
      password: '',
      username: '',
    }

    const {
      login,
    } = this.props;
  }



  loginHandler() {
    const { password, username } = this.state;
    const auth = Auth.isUserAuthenticated()
    const token = null;

    login(auth, token, password, username);   
  };

  render() {
    return (
      <div className="App">
        <div className="title">
          Recipe Box
        </div>
        <div className="form-inline login-form">
          <div className="form-group">
            <input
              className="form-control"
              onChange={e => this.setState({ username: e.target.value })}
              placeholder="Username"
            />
            <input
              className="form-control"
              onChange={e => this.setState({ password: e.target.value })}
              placeholder="Password"
              type="password"
            />
          </div>
          <button
            className="btn btn-success"
            onClick={() => this.loginHandler()}
            type="button"
          >
            Login
          </button>
        </div>
      </div>
    );
  }    
}

function mapDispatchToProps(dispatch) {
  console.log('dispatching:', dispatch)
  return bindActionCreators({login}, dispatch);
}

function mapStateToProps(state) {
  return { state }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

动作常量:

// AUTH
export const LOGIN = 'LOGIN';
export const LOGOUT = 'LOGOUT';

// USERS
export const ADD_USER = 'ADD_USER';
export const DELETE_USER = 'DELETE_USER';
export const UPDATE_USER = 'UPDATE_USER';

动作:

import {
  ADD_USER,
  DELETE_USER,
  LOGIN,
  LOGOUT,
  UPDATE_USER,
} from '../constants/constants';

export const login = (auth, token, password, username) => {
  const action = {
    type: LOGIN,
    auth,
    token,
    password,
    username,
  }
  console.log('login action:', action)
  return action;
}

减速机:

import {
  LOGIN,
  LOGOUT,
} from '../constants/constants';

const login = (action) => {
  console.log('hitting B')
  const { auth, token, password, username } = action;

  return {
    auth: auth,
    token: token,
    password: password,
    username: username,
  }
}

const authControl = (state = [], action) => {
  console.log('hitting C: ', action)
  let authControl = null;
  switch(action.type) {
    case LOGIN:
      authControl = [...state, login(action)]
      console.log('authControl:'. authControl);
      return authControl;
    default:
      console.log('hittibbng default', state)
      return state;
  }
}

export default authControl;

【问题讨论】:

  • 可能存在一系列可能性:#1 检查import reducer from './reducers/usersRedcuers'; 是否实际上正在导入authControl。 #2)检查你没有改变任何状态(你是否尝试在减速器中设置断点以确认它根本没有达到它?)

标签: reactjs redux


【解决方案1】:

好像你错过了调度到减速器

import {
  ADD_USER,
  DELETE_USER,
  LOGIN,
  LOGOUT,
  UPDATE_USER,
} from '../constants/constants';

export const login = (auth, token, password, username) => dispatch => {
  const action = {
    type: LOGIN,
    auth,
    token,
    password,
    username,
  }
  console.log('login action:', action)
  dispatch(action)
}

【讨论】:

    【解决方案2】:

    在 App.jsx 组件中,您应该使用作为道具传递给组件的操作,而不是直接调用该操作。

    loginHandler 应该如下所示:

      loginHandler() {
        const { password, username } = this.state;
        const auth = Auth.isUserAuthenticated()
        const token = null;
    
        this.props.login(auth, token, password, username);   
      };
    

    【讨论】:

    • 这成功了,谢谢!只是好奇,如果从 Actions 文件中导入函数,为什么需要将其作为 prop 调用?
    • 需要为 redux 分派操作以知道它必须更改存储。当您从操作文件中调用函数时,您只是创建了表示操作的对象,但 redux 不知道需要更改存储。我认为thisthis 会帮助你更好地理解它。
    猜你喜欢
    • 2018-11-08
    • 2022-01-13
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2019-02-21
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多