I've created a login page, where I'm using axios.get to retrieve information about a user from an AWS SQL database. Once retrieving this information, I would like to pass the user ID to an Analytics page. I'm building all of this on React.
I'm using redux to do this, but I'm having an issue with setting the userID inside the login page after getting the information from AWS. Here's how I'm getting the information from AWS and trying to dispatch this new userID into the redux store.
import { connect } from 'react-redux';
import { setUserId } from '../redux/methods.js'
axios.get(url+this.state.email)
.then(res => {
let userid = res.data.result.ID; //The user ID is in res.data.result.ID
this.props.dispatch(setUserId(userid));
})
export default connect()(Login)
But this does not change the userID inside the redux store. So when I'm accessing the value inside the analytics page, I'm still getting the default state.
Here is my redux code
The reducer
const userIdDefaultState = {
id: 0
};
export default (state = userIdDefaultState, action) => {
switch (action.type){
case 'SET_USER_ID':
return action.id;
default:
return state;
}
};
Set User ID Method
export const setUserId = (id = '') => ({
type: 'SET_USER_ID',
id
});
This is how I'm getting the data from the redux store inside the analytics page
let userID = this.props.id;
This is rendering the default information I'm providing inside the store, so I'm guessing it's the setUserId method that's not working.
Inside index.js, I tried this and it dispatches the information to the store correctly so I'm not sure why it isn't working inside of login.
store.dispatch(setUserId({ id: 2}));
Any tips would be very much appreciated. Thank you! :)