dispatch is not a function TypeError

10,496

You're executing the editUserNameDelegate function inside of render, instead of passing it as the onClick handler. You want onClick={editUserNameDelegate} instead.

Also, if you're writing it that way, you need to make sure that the function is bound up to dispatch when you call it. You can do that like:

export default connect(null, {editUserNameDelegate})(MyComponent)

And then pass onClick={this.props.editUserNameDelegate} in your render function.

Share:
10,496
cid
Author by

cid

Updated on June 28, 2022

Comments

  • cid
    cid almost 2 years

    I'm new to react-redux and I'm using the react-thunk middleware. I'm getting the error "Uncaught TypeError: dispatch is not a function" whenever I try to run my action creator (by pressing my button). Anyone know what's going on?

    src/actions/index.js

    function editUserName (newUserName) {
        return {
            type: 'CHANGE_USER_NAME',
            newUserName: newUserName
        }
    }
    
    export function editUserNameDelegate () {
        return function (dispatch, getState) {
            return dispatch(editUserName("thunkUser"))
        }
    }
    

    src/containers/admin.js

    import { editUserNameDelegate } from '../actions/index'
    ...
    <input type="button" onClick={ editUserNameDelegate() } value="edit UserName" />