简体   繁体   English

在组件中使用dispatch()时未调用Redux reducers

[英]Redux reducers not being called when using dispatch() in Component

I'm pretty new to react/redux (and javascript in general) so please bear with me on my use of the terminology here... 我对react / redux(和一般的javascript)相当陌生,因此请允许我继续使用此处的术语...

I am trying to understand how Components and Reducers work so currently I am practicing on a small app that I mostly copied/pasted from a tutorial. 我试图了解Components和Reducers的工作原理,所以目前我正在研究一个小型应用程序,该应用程序大多是从教程中复制/粘贴的。 The issue I am having is that I am trying to dispatch an action from my Component which alters the Redux state but I am not even seeing my console.log() messages inside my reducer function(s) when I dispatch an action from my Component. 我遇到的问题是我试图从Component调度一个动作,这会改变Redux状态,但是当我从Component调度一个动作时,我什至没有在我的reducer函数中看到console.log()消息。 。

This is what I currently have: 这是我目前拥有的:

TwApp.js TwApp.js

import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { TWAPP_USERDATA_AUTH_TOKEN } from '../Constants'
import { loginSuccess } from '../actions'


class TwApp extends Component {
    constructor(props) {
        super(props)
        this.handleChange = this.handleChange.bind(this)
        this.handleRefreshClick = this.handleRefreshClick.bind(this)
    }

    componentDidMount() {
        console.log("TwApp componentDidMount")
        this.props.loginSuccess() // This is where I want to dispatch an action
    }

    componentDidUpdate() {
    }

    handleChange() {
    }

    handleRefreshClick(e) {
        e.preventDefault()
        this.props.loginSuccess()
    }

    render() {
        const { loggedIn } = this.props;
        console.log("Rendering TwApp.")

        if (!loggedIn) {
            console.log("user not logged in. loggedIn = " + loggedIn)
        }
        else {
            return (
                <div>
                    <p>Success</p>
                </div>
            )
        }

    }
}


function mapStateToProps(state) {
    // nothing for now
}

function mapDispatchToProps(dispatch) {
    return {
        loginSuccess: () => { dispatch(loginSuccess) }
    }

}

export default connect(mapStateToProps, mapDispatchToProps)(TwApp)

actions.js actions.js

export const USER_LOGIN_SUCCESS = 'USER_LOGIN_SUCCESS'

export function loginSuccess() {
    return {
        type: USER_LOGIN_SUCCESS,
    }
}

reducers.js reducers.js

 // Contains a bunch of stuff that isn't being used yet
import { combineReducers } from 'redux'
    import {
        USER_LOGIN_SUCCESS, INVALIDATE_SUBREDDIT,
        REQUEST_POSTS, RECEIVE_POSTS
    } from './actions'



    function reducer1(state = {}, action) {
        console.log("reducer1: state =" + JSON.stringify(state) + ", action = " + JSON.stringify(action))
        switch (action.type) {
            case USER_LOGIN_SUCCESS:
                console.log("Reducer USER_LOGIN_SUCCESS")
                state.loggedIn = true
                return state
            case RECEIVE_POSTS:
            case REQUEST_POSTS:

            default:
                return state
        }
    }

    function reducer2(state = {}, action) {
        console.log("reducer2: state =" + JSON.stringify(state) + ", action = " + JSON.stringify(action))

        switch (action.type) {
            case INVALIDATE_SUBREDDIT:
            case RECEIVE_POSTS:
            case REQUEST_POSTS:
            default:
                return state
        }
    }


    const rootReducer = combineReducers({
        reducer1,
        reducer2
    })

    export default rootReducer

Neither reducer1's nor reducer2's console.log() message appears in console. reducer1或reducer2的console.log()消息都不会出现在控制台中。 When I call dispatch() from my TwApp component, do all reducers (reducer1 and reducer2) get called? 当我从TwApp组件调用dispatch()时,是否调用了所有的reducer(reducer1和reducer2)? Am I misunderstanding something? 我误会了吗?

Thanks 谢谢

You are dispatching 'loginSuccess' which is a function, or by redux terms an action creator. 您正在分派“ loginSuccess”(它是一个函数),或者用redux术语来分派一个动作创建者。
You should dispatch actions, which are plain objects, to your reducers. 您应该将动作(它们是简单的对象)分派给减速器。

What you want to do is dispatch the action that loginSuccess will create for you: 您要做的是调度loginSuccess将为您创建的操作:

loginSuccess: () => { dispatch(loginSuccess()) }

comonentDidUpdate as you have it already: comonentDidUpdate,因为您已经拥有它:

   componentDidMount() {
        console.log("TwApp componentDidMount")
        this.props.loginSuccess() 
    }

then remove your mapDispatchToProps function completely and export the following: 然后完全删除mapDispatchToProps函数并导出以下内容:

export default connect(mapStateToProps, {loginSuccess})(TwApp)

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

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