0

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);
      });
  }
};

This is what my console outputs

1 Answer 1

2

You have to return the array inside render (and wrap it in some tag):

return (
  <div>{ this.props.users.map((user) => {
      console.log("USER FOUND!");
      console.log(user.email);
      return <p key={user.id}>USER FOUND: {user.email}</p> }
  </div> 
 );
Sign up to request clarification or add additional context in comments.

5 Comments

I'd like to note that doing mapping inside the return for render is not good practice (or doing any kind of logic flow, for that matter). It's best to take all the logic out to before the return statement and then just use the variable name inside the return, with the JSX.
Yess! Thanks caisah :D
Hey nbkhope, what would that look like exactly? I'm having issues getting it set up the way you reccomended - Nevermind, got it!
I think that @nbkhope is suggesting the code structure I have in this example: gist.github.com/markerikson/47fff93c92286db72b22bab5b02e2da3 .
@markerikson yes, that's great! Thank you for sharing. JSX should look clean and nice. As an additional note, sometimes instead of having something before return, you can also pull the logic into its own separate function and call it from inside the JSX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.