0

Here's the problem with iterating over the components as array elements, I guess. I tried many iteration methods for loop, .forEach() and .map(). The some result was came only from the first one. It was rendered only one component from the array, when famous methods .forEach() and .map() wasn't rendered nothing at all.

I logged to the console the value of iterator variable from the above for loop and it wasn't incremented at all, it was 0, when the max count is 10.

Here's the code:

const renderState = () => {
    if (loading) {
      // When the users objects are loading from the server:
      return <p>Loading...</p>;
    } else if (hasErrors) {
      // When some loading-error occured:
      return <p>Can not load the page.</p>;
    } else {
      // And when loading was successfully finished, rendering all of them:
      for (let i = 0; i < users.length; i++) {
        return <ProfileCard key={users[i].id} user={users[i]} />
      }
    }
  };

3 Answers 3

1

I think only 1 item is rendering because you are returning something. "The return statement ends function execution and specifies a value to be returned to the function caller." - MDN docs

I've got a list in my app and that goes:

{orders.map((order) => ( 
    <Order key={order.Oid} data={order}/>
))}

So, try removing the return statement. And try it with .map

EDIT:

in the else statement:

return {users.map((user) => <ProfileCard />)}
Sign up to request clarification or add additional context in comments.

4 Comments

Ahh I see. Try return {users.map((user) => <ProfileCard />)}
Thanks a lot! It finally worked for me! God bless your heart! Thanks:))
No problem my friend, can you 'solve' the question? :)
Yes, It's solved and works fine well:)
1

The solution is to put the iterating function (for this case it's the .map() method) into the direct return statement of the React component:

return (
    <div>
      {
        users.map((user) => (
          <ProfileCard key={user.id} user={user} />
        ))
      }
    </div>
  );

Comments

1
 for (let i = 0; i < users.length; i++) {
        return <ProfileCard key={users[i].id} user={users[i]} />
      }

Here when for loop starts iterating it goes to this return line. And return inside a loop tells the loop to break the execution flow right at that point and return whatever you got.

That is the reason only 1 profile card appears.

Solution:

{array.map((item) => ( 
    <Component key={item.key} data={item.data}/>
))}

With this map method you are running a function on each iteration and return something on completion of each iteration.

So each iteration creates a new function which returns some Component. That is why this works even if you have a implicit return.

Comments

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.