0

I have a condition look like below

{finishedTodos ? <ListItem button divider className={classes.finishCategory} > <ArrowForward className={classes.checkIcon} /> <ListItemText primary={`Finished ${finishedTodos.length}`} style={{color:"tomato"}}/> </ListItem> : null } //--------------------------------------------- {finishedTodos.map((todo) =>( <ListItem button divider> <CheckCircle className={classes.checkIcon} /> <ListItemText primary={todo.title} style={{textDecoration:"line-through",color:"gray"}} /> </ListItem> ))} 

so right now what I want to do is I Want to combine these two , and when I tried to put {finishedTodos.map... below the </ListItem>,it reported ": expected" , so I think there's something wrong with formatting , do anyone know how to solve this ?

4
  • what you mean by combine ? Commented Aug 25, 2021 at 14:09
  • One thing you can do that is slightly better is use the && operator for the first condition. {finishedTodos && <YourCompoents... /> } Commented Aug 25, 2021 at 14:12
  • Probably because you were returning more than one element from the ternary. Move the mapping code back and wrap the whole block with a fragment (<><ListItem>...{mapcode}</>). Commented Aug 25, 2021 at 14:13
  • {finishedTodos.map((todo) => is not in same escope. Try if - else if and else Commented Aug 25, 2021 at 14:15

2 Answers 2

3

JSX requires returned code blocks to be wrapped in a single JSX element, like a <div>. You can also use simple JSX fragments <>...</> to wrap your code.

In this case, I would also recommend using a short circuit operator, &&, instead of a ternary operator ? ... :null for simple if {} statements.

{finishedTodos && <> <ListItem button divider className={classes.finishCategory} > <ArrowForward className={classes.checkIcon} /> <ListItemText primary={`Finished ${finishedTodos.length}`} style={{color:"tomato"}}/> </ListItem> {finishedTodos.map((todo) => <ListItem button divider> <CheckCircle className={classes.checkIcon} /> <ListItemText primary={todo.title} style={{textDecoration:"line-through",color:"gray"}} /> </ListItem> )} </> } 
Sign up to request clarification or add additional context in comments.

1 Comment

I like that your answer provides some explanation as to why this is the fix. Some small improvements: Even though you put HTML in quotes, I'd call it what it is: JSX since the two are already confusing to new React devs, but are really very different. I woudn't recommend the short circuit operator for most situations because of how it handles some falsy values (ie if finishedTodos is 0, it will actually render 0 instead of nothing).
2

Just update like it with Fragment

{ finishedTodos ? ( <> <ListItem button divider className={classes.finishCategory}> <ArrowForward className={classes.checkIcon} /> <ListItemText primary={`Finished ${finishedTodos.length}`} style={{ color: "tomato" }} /> </ListItem> {finishedTodos.map((todo) => ( <ListItem button divider> <CheckCircle className={classes.checkIcon} /> <ListItemText primary={todo.title} style={{ textDecoration: "line-through", color: "gray" }} /> </ListItem> ))} </> ) : null; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.