Redux Store(仅返回初始状态)不更新(Actions完全正常)

时间:2018-04-08 14:05:45

标签: reactjs typescript redux react-redux redux-thunk

验证完成后,我可以看到更新操作和减速器更新 - 但mapstatetoprops对新的reducer状态没有任何作用

商店会在每次更新时记录相同的状态(初始状态)

import React from 'react';
import { bindActionCreators } from 'redux'
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';
import { Redirect } from 'react-router-dom'
import firebase from 'firebase';
import { connect } from 'react-redux';
import { signIn, signOut } from '../reducer/actions'
import { auth } from '../firebase'

class LoginPage extends React.PureComponent {

  // Configure FirebaseUI.
  uiConfig = {'FirebaseUI Config'}

  componentDidMount = () => {
    auth.onAuthStateChanged((user) => { // gets user object on authentication
      console.log('OnAuthStateChanged', user)
      console.log('Check If Props Change in AuthChange', this.props.isauthed)

      if (user) {
        this.props.signIn(user)
      } else {
        this.props.signOut(user)
      }
    });
  }

  render() {
    console.log('Check If Props Change in Render', this.props.isauthed)
    if (!this.props.isauthed) {
      return (
        <div>
          <h1>My App</h1>
          <p>Please sign-in:</p>
          <StyledFirebaseAuth uiConfig={this.uiConfig} firebaseAuth={firebase.auth()} />
        </div>
      );
    }
    return (
      <Redirect to='/' />
    )
  }
}

export default (LoginPage);

应该派遣和更新道具的JS

import { connect } from 'react-redux';
import { signIn, signOut } from '../reducer/actions'
import { bindActionCreators } from 'redux'
import LoginPage from './LoginPage';

const mapStateToProps = (state) => {
  console.log('LOGINmapstatetoprops', state.Authed)
  return {
    isauthed: state.Authed.isauthed,
  }
}

const mapDispatchToProps = (dispatch) => {
  console.log('LOGINmapDISPATCHoprops')
  return bindActionCreators({signIn, signOut}, dispatch)
}


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

减速机

import LoginPage from '../components/LoginPage';
import firebase from 'firebase';

const initialState = {
  isauthed: false,
  error: ''
}

const AuthReducer = (state = initialState, action) => {
  console.log('this is an action', action)
  switch (action.type) {
    case 'IsSignedIn':
      return {
        ...state,
        isauthed: action.payload
      }
      break;
    case 'IsNotSignedIn':
      return {
        ...state,
        isauthed: action.payload
      }
      break;
    default: return state
  }
}

export default AuthReducer;

这是动作文件

export const signIn = (user) => {
  console.log('this is from actions', user)
  return {
    type: 'isSignedIn',
    payload: true
  }
}

export const signOut = (user) => {
  console.log(user)
  return {
    type: 'isNotSignedIn',
    payload: false
  }
}

Redux商店

import { createStore } from 'redux';
import logger from 'redux-logger';
import { createEpicMiddleware } from 'redux-observable';
import {AllReducers} from './reducer/index';
//import rootEpic from './modules/rootEpic';

//const epicMiddleware = createEpicMiddleware(rootEpic);

const store = createStore(AllReducers);

store.subscribe(() => {
  console.log('storeupdated', store.getState())
});

export default store;

CombineReducers功能

import {combineReducers} from 'redux'
import {AuthReducer} from './AuthReducer'
import {UserReducer} from './UserReducer'

export const AllReducers = combineReducers({
  Authed: AuthReducer 
  User:   UserReducer
})

1 个答案:

答案 0 :(得分:0)

好像你在'isSignedIn''IsSignedIn'之间有拼写错误(首字母不同)

您可以通过使用在分隔文件中声明的变量来避免这种情况。我喜欢称它为types.js的行为类型:

// types.js
export const IS_SIGNED_IN = 'isSignedInOrEvenWhateverButYouBetterKeepAClearName'
// (same with all your types)

然后您可以在其他文件中导入此变量。你不再有拼写错误了!

P.S:您可以在redux documentation

中找到所有这些想法