0
votes

I'm using react, redux-thunk, async action for signin gets a dispatch function like it should be async action for signout gets a class with properties of event like target etc when consoling the dispatch.

Navbar.jsx

const Navbar = ({ profile, history }) => {
    return (
        <nav>
            <Button type="danger" onClick={signOut(history)}>
               Logout
            </Button>
        </nav>
    )
}

const mapStateToProps = state => ({
    profile: state.firebase.profile,
})

const mapDispatchToProps = dispatch => ({
    signOut: history => dispatch(signOut(history)),
})

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(withRouter(Navbar))

Async Action

export const signIn = ({ email, password }, history) => {
    return (dispatch, getState) => {
        auth.signInWithEmailAndPassword(email, password)
            .then(() => {
                console.log('TCL: dispatch', dispatch) // returns dispatch function
                history.push('/')
                dispatch({ type: 'LOGIN_SUCCESS' })
            })
            .catch(err => {
                dispatch({ type: 'LOGIN_ERROR', err })
            })
    }
}

export const signOut = history => (dispatch, getState) => {
    auth.signOut()
        .then(() => {
            console.log('TCL: dispatch', dispatch) // return class and throws dispatch is not a function
            history.push('/login')
            dispatch({ type: 'SIGNOUT_SUCCESS' })
        })
        .catch(err => console.log(err))
}
2
When you say it returns a class, what class are you getting? - James
It has currentTarget, target properties, so I'm assuming it's event that's being passed for some reason. - prvnbist
@prvbist ah, can see the issue. - James

2 Answers

0
votes

Found the solution - I also needed to get the signOut from the props as well

import {signOut} from '../store/actions/authActions'
const Navbar = ({ profile, history, signOut }) => { // adding "signOut" solved it.
    return (
        <nav>
            <Button type="danger" onClick={() => signOut(history)}>
               Logout
            </Button>
        </nav>
    )
}
0
votes

You are calling the signOut function when hooking up the event handler which assigns the result to the onClick handler i.e.

onClick={signOut(history)}

Which means onClick would trigger (dispatch, getState) => ... and explains why dispatch === evt. You need to wrap your call with an event handler to swallow the click event:

onClick={() => signOut(history)}