简体   繁体   中英

Redux Uncaught type error: dispatched action is not a function

I feel like I'm missing something simple here, but for whatever reason this simple ErrorMessageAlert won't dispatch an action. I've dispatched many times and I can't seem to figure out this error.

Can anyone lend a second pair of eyes?

Here's my Component

import { connect } from 'react-redux';
import styled from 'react-emotion';
import { resetErrorMessage } from 'users/ducks'


const ErrorMessage = styled.div`
    width: 100%;
    color: red;
    position: fixed;
    background: #F9DADA;
    text-align: center;
    border-bottom: 1px solid red;
    padding-top: 5px;
    padding-bottom: 5px;
    z-index: 1;
`

export class ErrorMessageAlert extends Component<props> {

    state = {  isHidden: true }

    componentDidMount() {
        console.log("triggered")
        // debugger
        const { resetErrorMessageAction } = this.props
        resetErrorMessageAction()
    }

    render(){

        const {
            errorMessage,
        } = this.props

        console.log(this.state.isHidden)
            return (
                <div>
                    {
                        this.state.isHidden && <ErrorMessage>{errorMessage}</ErrorMessage>
                    }
                </div>
            )
    }
}

const mapDispatchToProps = (dispatch) => ({
    resetErrorMessageAction: () => dispatch(resetErrorMessage()),
})

export default connect(null, mapDispatchToProps)(ErrorMessageAlert);

in users/ducks

export const RESET_ERROR_MESSAGE = 'RESET_ERROR_MESSAGE';

export const resetErrorMessage = () => ({
    type: RESET_ERROR_MESSAGE,
})

Here's the main error message stack trace:

ErrorMessageAlert.js:29 Uncaught TypeError: resetErrorMessageAction is not a function
    at ErrorMessageAlert.componentDidMount (ErrorMessageAlert.js:29)
    at ErrorMessageAlert.componentDidMount (react-hot-loader.development.js:654)
    at commitLifeCycles (react-dom.development.js:17334)
    at commitAllLifeCycles (react-dom.development.js:18736)
    at HTMLUnknownElement.callCallback (react-dom.development.js:149)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
    at invokeGuardedCallback (react-dom.development.js:256)
    at commitRoot (react-dom.development.js:18948)
    at react-dom.development.js:20418
    at Object.unstable_runWithPriority (scheduler.development.js:255)
    ```

resetErrorMessageAction is undefined because I had imported it incorrectly from a different file since I was using an export defaulted function.

So you need to correctly import the file.

import { ErrorMessageAlert } from 'error/ErrorMessageAlert';

vs

import ErrorMessageAlert from 'error/ErrorMessageAlert';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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