Im am getting undefined with mapstatetoprops

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Im am getting undefined with mapstatetoprops



I am trying pass logged data from my redux actions to the front end but keep getting user.name of null or undefined.



This is the front end where I am simply trying to get user.name to appear so that it says Hi user.name(name of persons account).


import React, { Component } from "react";
import { NewPropertyForm, FormPageOne, FormPageTwo, FormPageThree,
FormPageFour } from "../../components/NewPropertyForm";
import { PropertyList } from "../../components/PropertyList";
import { Container, Button, Modal, ModalCard, ModalCardTitle,
ModalBackground, ModalCardFooter, ModalCardHeader, Delete, ModalCardBody
} from 'bloomer';
import StepZilla from "react-stepzilla";
import modal from "./modal-bg.svg";
import "./Manager.css";
import {login} from '../../actions/authActions'


import {connect} from 'react-redux';
import { bindActionCreators } from 'redux'

const steps =
[
{name: 'Step 1', component: <FormPageOne /> },
{name: 'Step 2', component: <FormPageTwo /> },
{name: 'Step 3', component: <FormPageThree /> },
{name: 'Step 4', component: <FormPageFour /> }
]

const modalBG = { backgroundImage: `url(${modal})` }

export class Manager extends Component {
// Setting our component's initial state
state = {
modal: "",
};

modalOpen = () => {
this.setState({ modal: "is-active" })
}



modalClose = () => {
this.setState({
modal: "",
login: "",
})
}


render() {
let { user } = this.props;
return (
<div className="manager">
<Container className="manager-container">
<div className="columns">
<div className="column">
<h1 className="title">Hi {user.name}</h1>
<h2 className="sub-title">You currently have 3 properties</h2>
<h2 className="sub-title">Check out the new applications you
received.</h2>
</div>
<div className="column user-dash-right">
<Button
isColor='primary'
className=""
onClick={this.modalOpen}><p>Create Listing</p></Button>

</div>

</div>

<h1 className="title has-text-centered">My Properties</h1>
<PropertyList />


<div className="new-property-modal">
<Modal className={this.state.modal}>

<ModalBackground />
<ModalCard style={ modalBG } >

<ModalCardBody>
<Delete onClick={this.modalClose} />

<div className='step-progress'>
<StepZilla
steps={steps}
showSteps={false}
nextButtonCls="button is-medium is-primary"
backButtonCls="button is-medium is-primary"
/>
</div>

</ModalCardBody>


</ModalCard>
</Modal>
</div>


</Container>
</div>
);
}
}

const mapStateToProps = ({auth}) => ({
user: auth.user,
authError: auth.authError
});

export default connect(mapStateToProps)(Manager)



This is the actions I have setup


import API from "../utils/API";
import { IS_AUTHENTICATED, AUTHENTICATION_FAILED } from
'../constants/actionTypes';

export const signup = ({name, email, phonenumber, password, role}) =>
async dispatch => {

try {
const {data} = await API.saveUser({
name,
email,
phonenumber,
password,
role

})
dispatch({
type: IS_AUTHENTICATED,
payload: data.user
})
console.log('--success', data);

} catch(error) {
console.error(error);
console.log('Come on work damnit')
}
}

export const login = ({email, password}) => async dispatch => {

try {
const {data} = await API.loginUser({
email,
password

})
dispatch({
type: IS_AUTHENTICATED,
payload: data.user
});
console.log('--success', data.user.name);
} catch(error) {
dispatch({
type: AUTHENTICATION_FAILED,
payload: "Invalid credentials, cannot login"
});
console.error(error);
}
}

export const getAuthenticated = () => async dispatch => {
try {
const {data, error} = await API.getAuthenticated();
console.log(data);
if(data) {
dispatch({
type: IS_AUTHENTICATED,
payload: data
});
} else {
console.log('ssss', error)
}
// if(getUser) login
//else logout
} catch(error) {
//window redirect to login
}
}


export const logout = () => async dispatch => {
try {
// const revoke = await API.logout()
dispatch({
type: IS_AUTHENTICATED,
payload: null
});
//should automatically display logout nav
//or redirect to anther page
} catch(e) {
//just refresh page
}
}



and these are my reducers


import {
IS_AUTHENTICATED,
AUTHENTICATION_FAILED
} from '../constants/actionTypes';

const initialState = {
user: null
}

const authReducer = (state = initialState, {type, payload}) => {
switch(type) {
case IS_AUTHENTICATED:
return {...state, user: payload, userInfo: payload}
case AUTHENTICATION_FAILED:
return {...state, user: null, authError: payload}
default:
return state
}
}

export default authReducer;



As you can see I tried to pass user.name but i keep getting cannot read property of null if I do const { user } = this.props
and i get cannot read property of undefined if i do const { user } = this.state.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

C++ virtual function: Base class function is called instead of derived