Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch is not a function TypeError

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" />
like image 787
cid Avatar asked May 06 '26 14:05

cid


1 Answers

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.

like image 157
markerikson Avatar answered May 09 '26 04:05

markerikson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!