-->

Uncaught TypeError: this.props.signinUser is not a

2020-06-23 08:26发布

问题:

src/actions/index.js

import axios from 'axios';
const ROOT_URL = "http://localhost:3090";

export function signinUser({ email, password }){
    return function(dispatch){
        axios.post(`${ROOT_URL}/signin`, { email, password });
        console.log("hey");
    }
} 

src/components/auth

import React, { Component } from 'react';
import { reduxForm } from 'redux-form';
import * as actions from '../../actions';
class Signin extends Component {


    handleFormSubmit({ email, password }){
            this.props.signinUser({ email, password });

        }

    render() {
        // code
        console.log(actions)
        const { handleSubmit, fields: { email,password } } = this.props;
        return (
        <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
            <fieldset className="form-group">
                <label>Email:</label>
                <input {...email} className="form-control" />
            </fieldset>
            <fieldset className="form-group">
                <label>Password:</label>
                <input {...password} className="form-control" />
            </fieldset>
            <button action="submit" className="btn btn-primary">Sign In</button>

        </form>
    );
    }

    // methods
}

export default reduxForm({
    form: 'signin',
    fields: ['email',  'password']
}, null, actions)(Signin);

I am using Redux Form to make a sign in form. This is the error I get when I Click on the 'Sign in' button. Maybe the signinUser() function is not properly defined.

bundle.js:29019 Uncaught TypeError: this.props.signinUser is not a function(…)

回答1:

In case anyone runs into it (I'm doing the same tut on Udemy now), this was a breaking change between redux-form 5.x and 6. Basically, you just have to run it through reduxForm and connect, as shown in this comment https://github.com/erikras/redux-form/issues/1050#issuecomment-221721848.

The end result is something like:

Sigin = reduxForm({ form: 'sigin', fields: [ 'email', 'password' ] })(Sigin);
export default connect(null, actions)(Sigin);


回答2:

You can view my github repo for the same problems you faced that i had successfully implemented using v6.5.0 of redux-form here. This is based on Stephen Grider's Advanced React and Redux Udemy course. If you like it, star it.

In regards to your current issue:

Redux Form v6 implement a somewhat different logic. Instead of fieldset you use Field and import it alongside your reduxForm:

import { reduxForm, Field } from 'redux-form';

As the new version does not have a built in connect method you will need to:

import { connect } from 'react-redux';

In your specific instance, the rest of your code should be refactored as follows:

const renderInput = field => {
    const { input, type } = field;
    return (
        <div>
            <input {...input} type={type} className="form-control" />
        </div>
    );
}

class Signin extends Component {
    handleFormSubmit({ email, password }) {    
        this.props.signinUser({ email, password });
    }

    render(){
        const { handleSubmit } = this.props;

        return (
            <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>

                <div className="form-group">
                    <label>Email:</label>
                    <Field name="email" 
                        type="email" component={renderInput} />
                </div>
                <div className="form-group">
                    <label>Password:</label>
                    <Field name="password" 
                        type="password" component={renderInput} />
                </div>
                <button action="submit" className="btn btn-primary">Sign in</button>
            </form>
        );
    }
}

function mapStateToProps(state) {
    return { form: state.form };
}

Signin = connect(mapStateToProps, actions)(Signin);
Signin = reduxForm({
 form: 'signin'
})(Signin);
export default Signin;

In the Redux Form migration guide you will see the proper new syntax that will allow you to remain updated.



回答3:

Try using actions.signinUser({ email, password });



回答4:

Change your redux-form dependency to version 5.3.3