I just started learning web dev and I'm a little confused by this one thing in ReactJS. In my App component's render function, if I return JSX without an overarching div:
render() {
return (
<h1 style={headerStyle}>Todo List</h1>
<Todo todos={this.state.todos}/>
);
}
I get an error:
./src/App.js
Line 29:9: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
27 | return (
28 | <h1 style={headerStyle}>Todo List</h1>
> 29 | <Todo todos={this.state.todos}/>
| ^
30 | );
31 | }
32 | }
but once I add a div:
render() {
return (
<div>
<h1 style={headerStyle}>Todo List</h1>
<Todo todos={this.state.todos}/>
</div>
);
}
}
everything works fine.
Why is the div necessary? Or if its not actually necessary, what else could I be doing wrong?