REACT Uncaught TypeError .then is not a function

夙愿已清 提交于 2019-12-23 19:23:08

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/42081085/react-uncaught-typeerror-then-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!