React complains about the following code with the error: Your render method should have return statement.
Could you please explain what is exactly wrong with my code inside ToDoItems.js?
ToDoItems.js
import React, { Component } from 'react';
class Kir extends Component {
state = {
todos: [
{
id: 1,
text: 'Take out the trash',
},
{
id: 2,
text: 'Grocery shopping',
},
{
id: 3,
text: 'Clean gecko tank',
}
]
};
render() {
const todoItems = this.state.todos.map((todoItem, index) => {
return (
<div key={index}>
<label>
<input
type="checkbox"
value={todoItem.text}
/>
{todoItem.text}
</label>
</div>
);
});
}
}
export default todoItems;
ToDo.js
import React, { Component } from 'react';
import todoItems from './ToDoItems';
class ToDo extends Component {
render() {
return (
<form>
{todoItems}
</form>
);
}
}
export default ToDo;
Top comments (3)
I'm guessing you're trying to export the ToDoItems component.
First change the name of your component accordingly so you can export/reuse it
then check your render method, this is no the good syntax, you're defining a function inside your render method which you should avoid at all cost. And React cannot access the return statement hence the warning.
Your render method should be:
Full code:
ToDoItems.js
ToDo.js
What a great solution to my problem. Thanks a lot.
I think in the file ToDoItems.js your return is inside "const todoItems = ...", so in the render method you are returning nothing. I am with mobile so sorry for the explanation.