I'm using react, redux, axios, and redux-thunk to handle async.
I have a feeling that the solution is going to be obvious but for some reason, I am unable to map over an array of users (retrieved from a local express/node api) and render each user out. The user's info will be logged into the console but the...
<p>NO USERS FOUND</p>
...component is rendered onto the screen instead.
Here is my repo: https://github.com/tedjames/reduxTest
And here is what my component looks like:
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { getUsers } from '../redux/actions'
class App extends Component {
componentWillMount() {
console.log(this.props);
this.props.getUsers();
}
render() {
if (this.props.users[1]) {
this.props.users.map((user) => {
console.log("USER FOUND!");
console.log(user.email);
return <p key={user.id}>USER FOUND: {user.email}</p>
});
} else {
return <p>NO USERS FOUND</p>
}
}
}
const mapStateToProps = state => {
return {
users: state.admin.users
};
};
export default connect(mapStateToProps, { getUsers })(App);
And this is my action that uses axios to fetch the users:
import axios from 'axios'
import { GET_USERS } from './types'
export const getUsers = () => {
return (dispatch) => {
axios.get('http://localhost:3090/users')
.then(res => {
console.log("<-- // DATA RECEIVED FROM SERVER // --> ");
console.log(res);
dispatch({ type: GET_USERS, payload: res.data })
})
.catch(({ response }) => {
console.log("// ERROR RECEIVED FROM SERVER //");
console.log(response);
});
}
};