I'm going through some coding examples, and came across this one example of a component declaration in React:
export const TodoList = ({ todos }) => (
<ul>
{
todos && todos.length
? todos.map((todo, index) => {
return <Todo key={`todo-${todo.id}`} todo={todo} />
})
: "No todos, yay!"
}
</ul>
);
I wanted to try and turn this ternary operator into an if/else statement, like this:
export const TodoList = ({ todos }) => (
<ul>
{
if (todos) && (todos.length) {
todos.map((todo, index) => {
return <Todo key={`todo-${todo.id}`} todo={todo} />
})
} else {
"No todos, yay!"
}
}
</ul>
);
But, I get the error at the line where the if
statement begins:
Parsing error: Unexpected token
why is it that the ternary operator works here but an if statement doesn't?