1

I've stumbled upon a trouble with rendering list items inside the component. What am I doing wrong and how to fix it?

This is what I've got in state

this.state = {
      tasksState: false,
      cupsState: false,
      ...
      citizensData: [
        {
          diamsEarned: 5,
          rubiesEarned: 6,
        }, ...
      ]
    }

Throphies are given as props

<Citizens tasksState={this.state.tasksState}
                        cupsState={this.state.cupsState}
                        trophies={this.state.citizensData} />
const Citizens = (props) => {
  let {
        tasksState,
        cupsState,
        trophies
      } = {...props};

  let CitizensList = [];

  for (let i=0; i<20; i++) {
    CitizensList.push(
      <div className="Citizens__container--block">
        <div className="box label-box">
          <label className="label-ct" htmlFor="citiz">Citizen {i+1}:</label>            
        </div>
        ...
        <div className="box diamond-box">
          <i className="fas fa-gem icon icon-diamond"></i>
          <p>{trophies[i].diamsEarned}</p>
        </div>
        <div className="box ruby-box">
          <i className="fas fa-gem icon icon-ruby"></i>
          <p>{trophies[i].rubiesEarned}</p>
        </div>
    );
}

  return (
    <div className="Citizens">
      <div className="Citizens__container">
          <CitizensList />
      </div>
    </div>
  );
}

As a result, list ain't rendered at all

1
  • change <CitizensList /> to { CitizensList } Commented Jun 8, 2020 at 15:58

2 Answers 2

1

You can replace this line:

<CitizensList />

with this:

{[...Array(20)].map(i => (
    <div className="Citizens__container--block" key={i}>
        <div className="box label-box">
          <label className="label-ct" htmlFor="citiz">Citizen {i+1}:</label>            
        </div>
        ...
        <div className="box diamond-box">
          <i className="fas fa-gem icon icon-diamond"></i>
          <p>{trophies[i].diamsEarned}</p>
        </div>
        <div className="box ruby-box">
          <i className="fas fa-gem icon icon-ruby"></i>
          <p>{trophies[i].rubiesEarned}</p>
        </div>
    </div>
))}

Though I recommend creating a component for the citizen.

Sign up to request clarification or add additional context in comments.

Comments

0

In react it better to use components, more components more flexibility. If I were to do the same task I would create another functional component for your

CitizensList

yet you can follow the below approach,

Here i = number of iteration and also you are missing a closing div.

const Citizens = (props) => {
  let {
        tasksState,
        cupsState,
        trophies
      } = {...props};    
 let i = 20;
  return (
    <div className="Citizens">
      <div className="Citizens__container">
          {

          Array.from(Array(i)).map((j) => 

            <div className="Citizens__container--block">
                <div className="box label-box">
                  <label className="label-ct" htmlFor="citiz">Citizen {j+1}:</label>            
                </div>
                ...
                <div className="box diamond-box">
                  <i className="fas fa-gem icon icon-diamond"></i>
                  <p>{trophies[j].diamsEarned}</p>
                </div>
                <div className="box ruby-box">
                  <i className="fas fa-gem icon icon-ruby"></i>
                  <p>{trophies[j].rubiesEarned}</p>
                </div>
            </div>  
        ))
    }
      </div>
    </div>
  );
}

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.