0

初期状態と init アクションのみを表示し、ディスパッチされたアクションとコードによって行われた状態変更を反映しない Redux 開発ツール拡張。一方、拡張機能でディスパッチャーを使用すると、アクションと状態の変化が表示されます。

私はこれに長い時間を費やしましたが、無駄に終わりました。

これが私のアクションクリエーターです。

import * as types from './actionTypes';
import UserApi from '../api/userApi';

export function loginUserSuccess(data) {
  return {type: types.LOGIN_USER_SUCCESS, data};
}

export function loginUserFailure(err) {
  return {type: types.LOGIN_USER_FAILURE, err};
}

export function loginUser(credentials, cb) {
  return function(dispatch) {
    UserApi.loginUser(credentials)
        .then(response => {
            dispatch(loginUserSuccess(response.data));
            cb();
        })
        .catch(err => {
            dispatch(loginUserFailure(err));
        });
     };
 }

私の減速機。

import * as types from '../actions/actionTypes';
import initialState from './initialState';

export default function userReducer(state = initialState.User, action) 
{
 switch (action.type) {
    case types.LOGIN_USER_SUCCESS: 
        {
            return Object.assign({},state,{isAuthenticated: true});
        }

    default: 
        return state;
 }
}

店舗構成。

import {
 createStore,
 applyMiddleware,
 compose
} from 'redux';
import rootReducer from '../reducers';
import reduxtImmutableStateInvariant from 'redux-immutable-state-invariant';
import thunk from 'redux-thunk';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
                    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({latency: 0 }) : compose;
export default function configureStore(initialState) {
  return createStore(
   rootReducer,
   initialState,
   composeEnhancers(
    applyMiddleware(thunk, reduxtImmutableStateInvariant())
   )
  );
}

これは、アクションが最初にディスパッチされる場所です。

import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {
  Redirect
} from 'react-router-dom';
import * as userActions from '../../actions/userActions';
import PropTypes from 'prop-types';

class Login extends React.Component {
 constructor(props, context) {
  super(props, context);
  this.state = {
   redirectToReferrer: this.props.user.isAuthenticated
  };
  this.login = this.login.bind(this);
 }

 login() {
  this.props.actions.loginUser({username: "a.p.dhanushu@gmail.com",password: "dhanushu"}, () =>{this.setState({redirectToReferrer : true});});}

 render() {
   const { from } = this.props.location.state || { from: { pathname: '/' } };
 const {redirectToReferrer} = this.state;
 let isAuth = this.props.user.isAuthenticated;

   if (redirectToReferrer) {
    return (
     <Redirect to={from}/>  
    );
  }

 return (
    <div>
      <p>You must log in to view the page at {from.pathname} and you are {isAuth ? "authenticated" : "not authenticated"}</p>
      <button onClick={this.login}>Log in</button>
    </div>
  );
 }
}

Login.propTypes = {
  actions: PropTypes.object.isRequired,
  location: PropTypes.object.isRequired,
  user: PropTypes.object.isRequired
};

function mapStateToProps(state, ownProps) {
 return {
 user: state.user
 };
}
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(userActions, dispatch)
  };
 }

 export default connect(mapStateToProps, mapDispatchToProps)(Login);
4

0 に答える 0