I am trying to lean react by solving a problem in react. Providing the react code challenge below.
I am trying to iterate the array and trying to print state and checkbox.
I googled but still no luck, not sure how to solve it. Providing my code below and stackblitz. Can you guys help me.
Type ‘void[]’ is not assignable to type ‘ReactNode’.
import { useState } from 'react';
import './stateDropdown.css';
import { states } from './States';
export function StateDropdown() {
const [isDropDownDisplayed, setIsDropdowndisplayed] = useState(false);
return (
<fieldset className="state-dropdown">
<button
className=""
onClick={() => setIsDropdowndisplayed((prevState) => !prevState)}
>
-- Select your states --
</button>
{isDropDownDisplayed && (
<div className="panel">
{states.map((state) => {
<>
<input type="checkbox" />
<label>{state.name}</label>
</>;
})}
</div>
)}
</fieldset>
);
}
2
Answers
Your syntax is incorrect here,
map
should simply return your component like:I would also recommended applying the necessary
key
property to your fragment to ensure that all children in a list have a unique identifier for re-rendering.Is a function that returns
<span>Hi</span>;
is also a function that returns
<span>Hi</span>;
is also a function that returns
<span>Hi</span>;
Is a function that returns NOTHING