In
If you must utilize a
export const SurveyResults = () => {
return (
<>
<p>How satisfied are you with our service?</p>
<ol type="1">
{
(
() => {
let li = [];
let lines = 10
while (lines > 0) {
li.push(<li key={lines}><input type="radio" name="score" /></li>)
lines --
}
return li
}
)()
}
</ol>
</>
)
}
I am rendering ten radio buttons to represent a survey satisfaction score.
I use an Immediately Invoked Function Expression (IIFE) to create a list of <li> elements with radio buttons. The IIFE is a JavaScript function defined and executed immediately after creation.
In this case, it runs a
Here is the exact implementation done with a
export const SurveyResults = () => {
return (
<>
<p>How satisfied are you with our service?</p>
<ol type="1">
{
(
() => {
let li = [];
for (let i = 1; i <= 10; i++) {
li.push(<li key={i}><input type="radio" name="score" /></li>);
}
return li;
}
)()
}
</ol>
</>
)
}
The correct way is to create the elements I want to render inside a loop and then return the resulting array in the
export const SurveyResults = () => {
const renderRadioButtons = () => {
let li = [];
for (let i = 1; i <= 10; i++) {
li.push(<li key={i}><input type="radio" name="score" /></li>);
}
return li;
}
return (
<>
<p>How satisfied are you with our service?</p>
<ol type="1">
{renderRadioButtons()}
</ol>
</>
)
}