1

I have a function I am attempting to call in my componentDidMount function. It is function is declared in my mapDispatchToProps function. I am trying to receive variables from the function to display in my render. Instead, I get this error:

TypeError: this.props.pageReload(...).then is not a function

Any advice would help to resolve this issue and hopefully not replicated. I have tried a couple of solutions I ran into but none successful.

Component Level:

import React from 'react';
import { connect } from 'react-redux';
import { Collapse, Container, Navbar, NavbarBrand, NavbarToggler, NavItem, NavLink } from 'reactstrap';
import { Link } from 'react-router-dom';
import { userActions } from '../../../../actions/user.actions';

class InnerHeaderComponent extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isOpen: false,
            First: "",
            Last: ""
        };
        this.toggle = this.toggle.bind(this);
    }
    componentDidMount() {      
        this.props.pageReload()
            .then((res) => {
                this.setState({
                    First: res.FirstName,
                    Last: res.LastName
                });   
            });                    

    }
 render() {
  return();
}
function mapStateToProps(state) {

    const { authentication } = state;
    const { user } = authentication;

        return {
        user
    };
}
const mapDispatchToProps = dispatch => ({
    pageReload: () => dispatch(userActions.pageReload())
});
export default connect(mapStateToProps, mapDispatchToProps)(InnerHeaderComponent);

Actions:

function pageReload() {
    return dispatch => {        
        const user = JSON.parse(localStorage.getItem('user'));        
        userService._setUserSession(user);
        dispatch({ type: userConstants.LOGIN_SUCCESS, user });
        console.log(user);
        return { FirstName: user.FirstName, LastName: user.LastName };
    };
}
7
  • Can you share a sandbox? Commented Apr 29, 2019 at 18:08
  • I am not sure how to set that up. Commented Apr 29, 2019 at 18:11
  • https://codesandbox.io/s/new Commented Apr 29, 2019 at 18:13
  • codesandbox.io/s/74946z17j0 Commented Apr 29, 2019 at 18:35
  • I have another error on there I am working to resolve. Commented Apr 29, 2019 at 18:35

1 Answer 1

2

This happens normally when you are not returning a promise from your function. It does't look like your returning a promise from your pageReload() function therefore your .then() is not valid and you are getting the error. Take a look at Javascript Promise.protoype.then() Try something like the following:

function pageReload() {
    return dispatch => {        
        const user = JSON.parse(localStorage.getItem('user'));        
        userService._setUserSession(user);
        dispatch({ type: userConstants.LOGIN_SUCCESS, user });
        return Promise.resolve(user)
    };
}

and then you can use then() like so:

this.props.pageReload()
.then(user => {
  console.log(user)
});

Note: this is not tested just assuming that's the issue

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.