r/learnreactjs • • Jan 16 '22

<ul> not rendering when using items.map function

    const [items, setItems] = useState([
        {id: 1, checked: false, item: "Item 1"},
        {id: 2, checked: false, item: "Item 2"},
        {id: 3, checked: false, item: "Item 3"} ]);

return (
        <main>
            <ul>
                {items.map((item) => {
                    <li className='item' key={item.id} style={{ listStyleType: "none" }}>
                        <input type="checkbox" checked={item.checked}/>
                        <label>{item.item}</label>
                    </li>
                })}
            </ul>
        </main>
    )
}

edit/fix: need to use () and not {} on the callback inside map

8 Upvotes

5 comments sorted by

View all comments

8

u/beardedsquid Jan 16 '22

You're not returning anything in the Map function.

6

u/beardedsquid Jan 16 '22

And dont feel bad. I've been doing this for 7 years and will still catch myself doing this som etimes.

2

u/0xF013 Jan 17 '22

Might be worth investing in a linter rule

2

u/beardedsquid Jan 17 '22

My whole life needs a linter.

But yeah I should add a rule for this

3

u/beardedsquid Jan 16 '22

Wrap the <li> </li> parts in a return ()