6

i'm doing function in my react component like this :

const Logged = (props) => {

  const doLogout = () => {
    props.logout().then(() => browserHistory.push('/'));
  }

  return(
    <IconMenu
      {...Props}
      iconButtonElement={
        <IconButton><MoreVertIcon /></IconButton>
      }
      targetOrigin={{horizontal: 'right', vertical: 'top'}}
      anchorOrigin={{horizontal: 'right', vertical: 'top'}}
    >
      <MenuItem primaryText="Sign out" 
        onTouchTap={doLogout}
      />
    </IconMenu>
  )
};

i already wrap the dispatch in the component and connect it.

const mapDispatchToProps = (dispatch) => {
    return {
        logout: () => (
            dispatch(logoutUser)
        )
    }
}

this is my action:

export function logoutUser(){
    return (dispatch) => {
        return new Promise((resolve) => { 
            dispatch({
                type    : LOGOUT_USER,
            });
            resolve();
        })
    }
};

and this is my reducer :

case LOGOUT_USER :  
            return Object.assign({}, state, {autenticated : false});

i always got this error

Uncaught TypeError: props.logout(...).then is not a function at Object.doLogout [as onTouchTap] (eval at

ukiyakimura
  • 151
  • 1
  • 5
  • 15

1 Answers1

2

I think you meant

function mapDispatchToProps(dispatch) {
    return {
        logout: logoutUser(dispatch)
    };
}

export function logoutUser(dispatch) {
    return () => {
        return new Promise((resolve) => { 
            dispatch({
                type: LOGOUT_USER,
            });
            resolve();
        });
    };
}

Although it doesn't look like there's anything asynchronous happening, so no reason to wait for anything.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thx a lot. it work. but i still don't get it, could you explain me a bit why it doesn't work before. in my vs code i see my action logoutUser would return promise. and i'm doing async because it need to change the authentication to false then redirect it to "/" ,is there a better way?@Bergi – ukiyakimura Feb 07 '17 at 04:12
  • In your code `logoutUser` does not return a promise, but a function; and it's not even called, but only passed to `dispatch` (whose result does get returned, and is not a promise). – Bergi Feb 07 '17 at 04:14