简体   繁体   中英

Redux - Dispatching Action (onClick Event)

I am simply trying to connect() my LoginPage (component) to my Redux Store and dispatch in action via a onClick (event) . When I console.log(this.props) my dispatch handler login() isn't in the component's props.

GitHub Repo -- https://github.com/jdavis-software/demo.git

Question: Why isn't my Redux Store either connection or dispatching the actions?

LoginPage:

import React, { Component} from 'react';
import {  connect } from 'react-redux';

export class LoginPage extends Component<any> {
    render(){
        console.log('props doesnt have contain - login(): ', this.props)
        return (<button onClick={ () => '' }>Login</button>)
    }
}

const mapProps = state => ({ user: state.user })

const dispatchProps = (dispatch) => {
    return {
        login: () => dispatch({ type: 'USER_LOGGED_IN', payload: true})
    } 
}

export default connect(mapProps,dispatchProps)(LoginPage)

Redux Configuration:

import { IStore, IUser } from '@interfaces';
import { createStore, combineReducers } from 'redux';
import ReduxPromise from 'redux-promise';
// reducers
import userReducer from './user.reducer';

// define the intial global store state
const initialState:IStore = {
    user: {
        isAuthenticated: false
    }
}

const appReducer = combineReducers({user: userReducer}) 

export default createStore(appReducer,initialState);

User Reducer:

// initial state
const initalState:IUser = {
    isAuthenticated: false
}

// reducer
const userReducer = (state:IUser = initalState, { type, payload}: IPayload): IUser => {
    console.log('user reducer start', state)
    switch (type) {
        case 'USER_LOGGED_IN':
            state = { ...state, isAuthenticated: payload }
            break;
        default:
            return state;
    }
    return state;
};
export default userReducer;

Root Page:

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
// styles
import './index.scss';
// pages
import { App } from '@pages';
// store
import store from './core/store/store';

render(
    <Provider store={store}>
        <App/>
    </Provider>, document.getElementById('app')
);

I checked your code on git repository. I found out that you're exporting the named export

export class LoginPage

and the default export,

export default connect(mapProps,dispatchProps)(LoginPage)

But when you're accessing it, you're accessing it as

import { /*Other components*/ , LoginPage } from '@pages'

So it is actually taking the named exported component which is not connected to store.

I suggest you to import as

import LoginPage , { /*Other components*/ } from '@pages'

This might solve your problem.

Return statements are missing in the properties of connect.

const mapProps = state => { return {user: state.user} }

const dispatchProps = (dispatch) => { 
  return {
    login: () => dispatch({ type: 'USER_LOGGED_IN', payload: true})
   }
}

export default connect(mapProps,dispatchProps)(LoginPage)

Updated: Please check Redux-dispatch

try:

import React, { Component} from 'react';
import {  connect } from 'react-redux';

export class LoginPage extends Component<any> {
    render(){
        console.log('props doesnt contain - login(): ', this.props)
        return (
            <button onClick={ this.props.login }>Login</button>
        )
    }
}

const mapProps = state => ({ user: state.user })

const dispatchProps = (dispatch) => ({ 
    login: () => dispatch({ type: 'USER_LOGGED_IN', payload: true})
})

export default connect(mapProps,dispatchProps)(LoginPage)

to return an object with Arrow Functions you need to wrap your {} with ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM