Answer the question
In order to leave comments, you need to log in
Why isn't Redux's action being called?
Why can't I call action on button click. The error Cannot read property 'props' of undefined falls into the console . I don't understand what I'm doing wrong.
Component code:
import React, {Fragment} from 'react';
import Nav from './Nav';
import { connect } from 'react-redux';
import { Login } from '../actions/user';
class Profile extends React.Component {
handleLogin() {
this.props.LoginAction();
}
render() {
return(
<Fragment>
<h1>Profile !!!</h1>
<Nav/>
<button onClick={this.handleLogin}>LOGIN</button>
</Fragment>
)
}
}
const mapStateToProps = store => {
return {
isLogin: store.user.isLogin,
isLoaded: store.content.isLoaded,
}
}
const mapDispatchToProps = dispatch => {
return {
LoginAction: () => dispatch(Login())
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Profile);
const initialState = {
isLogin: false
}
export function userReducer(state = initialState, action) {
switch(action.type) {
case 'LOGIN':
return {
...state, isLogin: !state.isLogin
}
default:
return state
}
}
export function Login() {
return {
type: 'LOGIN'
}
}
Answer the question
In order to leave comments, you need to log in
Try replacing handleLogin with an arrow function
handleLogin = () => {
this.props.LoginAction();
}
onClick={this.handleLogin.bind(this)}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question