Problem with iteration in React

Asked

Viewed 27 times

0

I’m consuming an API, and putting your data in a 'li', only it’s popping the page because I mapped all the api items:

function showState(states){
    return states.map(state => <li key={state.nome}>{state.nome} - {state.sigla}</li>)
}

...

<div className="center">
            <ul>
                {response.data? showState(response.data): response.err}
            </ul>
 </div>

The idea would be that the return of the showState function would generate something like, but dynamically, limiting 9 'li' by 'ul':

<ul>
   <li>key={state[0].nome}>{state[0].nome} - {state[0].sigla}</li>
   <li>key={state[1].nome}>{state[1].nome} - {state[1].sigla}</li>
   <li>key={state[2].nome}>{state[2].nome} - {state[2].sigla}</li>
   ...
   <li key={state[8].nome}>{state[8].nome} - {state[8].sigla}</li>
</ul>
<ul>
   <li>key={state[9].nome}>{state[9].nome} - {state[9].sigla}</li>
   <li>key={state[10].nome}>{state[10].nome} - {state[10].sigla}</li>
   <li>key={state[11].nome}>{state[11].nome} - {state[11].sigla}</li>
   ...
   <li key={state[16].nome}>{state[16].nome} - {state[16].sigla}</li>
</ul>
<ul>
   <li>key={state[17].nome}>{state[17].nome} - {state[17].sigla}</li>
   <li>key={state[18].nome}>{state[18].nome} - {state[18].sigla}</li>
   <li>key={state[19].nome}>{state[19].nome} - {state[19].sigla}</li>
   ...
   <li key={state[26].nome}>{state[26].nome} - {state[26].sigla}</li>
</ul>

1 answer

0


If I understand correctly, you need to filter the amount of elements to not render everything at once. The easiest way to do this would be to maintain the state of the indexes you want and use the function filter on your list states, something like:

const filteredStates = states.filter((state, index) => { if (index >= indexStart && index < indexLimit) return state; });

And pass the filteredStates in the call of its function showState. If there is an event triggered by the user, for example, you can update the values of the variables indexStart and indexLimit calling the function again. This causes only index items within the set limit to return.

I hope I’ve helped in some way.

  • It worked well friend, thanks! The idea was to do something more dynamic (without having to create the <ul>) but I’ll learn over time kkkk. A question if I want to leave the const filteredStates out of function showState i would have to create the variables indexStart and indexLimit and change them by useState() right? And in case I passed the const filteredStates as an argument to function showState() and also the arguments of indexStart and indexLimit(in that way showState(filteredStates,0,9)) would the communication between the constant and the other arguments?? (I do not know if I was clear...)

  • No need to re-create the ul, you can just render the ul your case indexStart either 0 or already leave the ul on the page and return the li gradually. About the useState, that’s right. The idea is to control their state in the component with useState and only pass what you want back to function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.