The following code seems to work as expected: (fiddle)
const headers = [
{
id: 'section-1',
header: 'section 1',
},
{
id: 'section-2',
header: 'section 2',
},
{
id: 'section-3',
header: 'section 3',
children: [
{
id: 'section-31',
header: 'section 31',
children: [
{
id: 'section-311',
header: 'section 311',
},
{
id: 'section-312',
header: 'section 312',
},
],
},
{
id: 'section-32',
header: 'section 32',
},
],
},
{
id: 'section-4',
header: 'section 4',
},
];
const Nav = ({ headers, className }) =>
<ul className={className}>
{headers.map((header) => [
<li className="list-group-item">
{header.header}
</li>,
(header.children && header.children.length) ? (
<Nav className="list-group" headers={header.children} />
) : null,
])}
</ul>
const Toc = () =>
<div id="my-super-interesting-nav">
<Nav className="list-group nav-root" headers={headers} />
</div>
I the future the headers
array will be dynamic. I have three questions:
Why doesn't React complain that I haven't set the key
prop on the child of the headers.map()
call in Nav
?
How bad will this confuse the reconciliation algorithm, given that the second assumption in the React reconciliation algorithm no longer holds?
If I wanted to specify a key in this case, how would I do that?
` but I think the section on permitted content there needs to be updated. But the main reason I'm putting them alongside is because
– Lars Nyström Oct 21 '16 at 14:19that's how [bootstrap list groups](http://getbootstrap.com/components/#list-group) worksbecause I saw some example on how to nest bootstrap list groups that way.