I am having difficulty dynamically rendering this list using Semantic UI. I'm getting an unexpected token error, but I'm not really sure of how to make this work. Both users_titles and users_entries are arrays set in state, but mapped from redux, so they are the same length. The (non-working) code is below. I simply want a list ordered as (title-1..entry1, title2..entry2, etc ).
It looks like calling another component just to create the list seems unnecessary (and I'm still not really sure how it would work any better). I'm very new to react and JS, so any help would be appreciated. Thanks!
class UsersPosts extends React.Component
...
displayListPosts = () => {
for (let index = 0; index < this.state.users_entries.length; index++) {
//debugger
<List.Item>
<List.Icon name='file outline' />
<List.Content>
<List.Header >
{this.state.users_titles[index]}
</List.Header>
<List.Description>
{this.state.users_entries[index]}
</List.Description>
</List.Content>
</List.Item>
}
}
...
render() {
const { loaded } = this.state
if (loaded) {
return (
<List>
{ this.displayListPosts() }
</List>
)
UPDATE:
After getting help from the accepted answer, the working code looks like this:
displayListPosts = () =>
this.props.posts.map((el) => (
<List.Item>
<List.Icon name='file outline' />
<List.Content>
<List.Header >
{el.title}
</List.Header>
<List.Description>
{el.entry}
</List.Description>
</List.Content>
</List.Item>
));
, where posts are a prop, in the form:
[ {id:1,title:'Quis.',entry:'Lorem'...},
{id:2,title:'Consequatur.',ent…:31.999Z'},
{id:3,title:'Laboriosam.',entr…:32.004Z'},
{id:4,title:'Eum.',entry:'Eaqu…:32.010Z'},
{id:5,title:'Reiciendis.',entr…:32.015Z'},
{id:6,title:'Nemo.',entry:'Qui…:32.020Z'},...]