Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Redux: 'X is not a function' after using mapDispatchToProps

Very new to Redux. I'm following an example at my work place to try and create a Facebook Login button using a stateless component. I can't seem to do the basic thing of binding the clickHandler property to a function of any description. I've reduced it to a simply console log in the below example. Whatever I do, when I click the button I get 'clickHandler is not a function'. What am I doing wrong?

Component

import React from 'react';

const FacebookLoginComponent = (props) => {
    const {
        clickHandler
    } = props;
    return(
        <button className="btn btn-primary" onClick={ () => clickHandler() }>Say hello</button>
    )
}
export default FacebookLoginComponent;

Container

import { connect } from 'react-redux';
import FacebookLoginComponent from "../components/FacebookLogin";

const mapStateToProps = state => ({});
const mapDispatchToProps = dispatch => ({
    clickHandler: () => console.log('hello')
})

const FacebookLogin = connect(
    mapStateToProps,
    mapDispatchToProps,
)(FacebookLoginComponent);

export default FacebookLogin;
like image 765
Chris Wickham Avatar asked Mar 27 '26 03:03

Chris Wickham


1 Answers

I suspect you might be using the unconnected FacebookLoginComponent by mistake instead of the connected FacebookLogin.

If you were using the FacebookLoginComponent and not passing in any props then clickHandler would indeed not be a function.

like image 172
Aron Avatar answered Mar 29 '26 20:03

Aron



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!