I have a function that walks down a simple tree and calls functions at each step to print a menu:
renderMenuFx(menu, renderFx) {
renderFx.startMenu()
for (const entry of menu) {
renderFx.startEntry()
renderFx.renderEntry(entry.path.join(""), entry.title)
if (entry.menuEntries) {
this.renderMenuFx(entry.menuEntries, renderFx)
}
renderFx.endEntry()
}
renderFx.endMenu()
}
and it works when logging to console:
this.renderMenuFx(tree, {
startMenu() { console.log('<ul>') },
endMenu() { console.log('</ul>') },
startEntry() { console.log('<li>') },
endEntry() { console.log('</li>') },
renderEntry(path, title) { console.log(`<a href="${path}">${title}</a>`) },
})
But it doesn't when I try to render it in React:
<div>
{
f.renderMenu(this.tree, {
startMenu() { return ( <ul> ) },
endMenu() { return ( </ul> ) },
startEntry() { return ( <li> ) },
endEntry() { return ( </li> ) },
renderEntry(path, title) { return ( <a href={path}>{title}</a> ) },
})
}
</div>
Actually, the issue is that it doesn't compile, giving:
./src/components/Menu/MenuDyn.js
Syntax error: Unexpected token (141:37)
139 | f.renderMenu(this.tree, {
140 | startMenu() { return ( <ul> ) },
> 141 | endMenu() { return ( </ul> ) },
| ^
I'm obviously doing it wrong: what's the correct way to handle such situation?