0

I can't understand how to use "items" property of React Semantic UI "List" component. Is there a way to pass it a function or a component it will use to render every item ?

<List items={repos} component={RepoListItem} />;
const RepoListItem = props => {
  return (
    <List.Item>
      <List.Content>{props.full_name}</List.Content>
    </List.Item>
  );
};

I look for a property like "component" here that could be used to set a rendering function or component.

1 Answer 1

1

You should be able to pass React Components an array of Child components if the parent allows it. Looking at the docs, this very much seems possible.

https://react.semantic-ui.com/elements/list/#types-basic

The example below comes straight from the docs

import React from 'react'
import { List } from 'semantic-ui-react'

const ListExampleBasic = () => (
  <List>
    <List.Item>Apples</List.Item>
    <List.Item>Pears</List.Item>
    <List.Item>Oranges</List.Item>
  </List>
)

export default ListExampleBasic

So what you want to do is generate an array of components for List.Item. based on the items state you provide. This can be achieved using the .map function.

import React from 'react'
import { List } from 'semantic-ui-react'

const RenderList = () => 
  <List>
    {repos.map((item) => RepoListItem(item))}
  </List>

const RenderRepoListItem = item => 
  <List.Item>
    <List.Content>{item.full_name}</List.Content>
  </List.Item>

As JSX has an XML-like syntax for compositional components, any children components are nested inside their parent.

Also, I'm not sure where List.Content is coming from, you could probably omit that and just have {item.full_name} directly inside List.Item

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

2 Comments

Thanks Carl ! That's the solution I finally used.Anyway I don't understand what this "items" property is useful for. Maybe a shortaround for some cases where you just want to display the description as item ?
Looks like it will generate the List.Item array of child components for you if you pass it an array of state objects.

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.