0

I have data coming from API

[
    {
      title: "Lesson 1",
      topics: [
        "Topic 1",
        "Topic 2",
        "Topic 3"
      ]
    },
    {
      title: "Lesson 2",
      topics: [
        "Topic 1",
        "Topic 2",
        "Topic 3"
      ]
    }
  

]

I want To show Lessons and Relevant Topics in nested Order Like Topic 1 -> Lesson 1 ,2 3 4 I am Doing Like this

<List>
                    {LessonList.map(() => (lesson)(
                        <ListItem button className={classes.LessonTitle} key={lesson.title} >
                            <ListItemIcon ><FiberManualRecordOutlinedIcon /></ListItemIcon>
                            <ListItemText primary={lesson.title} />
                        </ListItem>

                        {
                            lesson.topics.map(() => (t) {
                                return (
                                    <ListItem button className={classes.LessonTopics} key='Django Learning'>
                                        <ListItemIcon ><FiberManualRecordIcon /></ListItemIcon>
                                        <ListItemText primary='Django Learning' />
                                    </ListItem>
                                )
                            }
                            )

                        }
                    ))}
</List>

But its Syntax Error

1
  • I don't understand your requirements. If you want to map array items to react elements you can do it like so: array.map((item) => { return <ListItem>{item.title}</ListItem>}) Commented May 12, 2021 at 13:16

2 Answers 2

2

JSX expression must have only one parents that's why it was showing error

Try something like below:-

      <List>
        {LessonList.map(lesson => (
          <>
            <ListItem button className={classes.LessonTitle} key={lesson.title}>
              <ListItemIcon>
                <FiberManualRecordOutlinedIcon />
              </ListItemIcon>
              <ListItemText primary={lesson.title} />
            </ListItem>
            {lesson.topics.map(topic => {
              return (
                <ListItem
                  button
                  className={classes.LessonTopics}
                  key="Django Learning"
                >
                  <ListItemIcon>
                    <FiberManualRecordIcon />
                  </ListItemIcon>
                  <ListItemText primary={topic} />
                </ListItem>
              );
            })}
          </>
        ))}
      </List>
Sign up to request clarification or add additional context in comments.

Comments

1

When you have more than 1 stuff coming out of a {} block in react, you need to wrap them in a div or something, but you can use react fragments like this <> </>

for example your code can be done like this:

<List>
  {LessonList.map((lesson) =>
      <>
        <ListItem button className={classes.LessonTitle} key={lesson.title}>
          <ListItemIcon>
            <FiberManualRecordOutlinedIcon />
          </ListItemIcon>
          <ListItemText primary={lesson.title} />
        </ListItem>

        {lesson.topics.map((t) => (
          <ListItem button className={classes.LessonTopics} key='Django Learning'>
            <ListItemIcon>
              <FiberManualRecordIcon />
            </ListItemIcon>
            <ListItemText primary='Django Learning' />
          </ListItem>
        ))}
      </>
  )}
</List>

also as you see your second map had syntax issue which I fixed that as well.

3 Comments

Can you explain this syntax LessonList.map(() => lesson(...))?
Its prolly a function that has a jsx element as argument.
i think the lesson must be inside the map parameter right? LessonList.map(lesson)=>()

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.