In a React class component, I'm writing a method that builds a <select>
dropdown and Babel is giving an error. The error is caused by the first <select>
tag not being closed. I can't figure out the correct react-jsx syntax for this.
In the code below, I'm trying to encapsulate the code that creates the <select>
into another method in the component class. If I have the function just build the options and put the <select>
and </select>
around that function (up in the render method) it works, but I want to put the selected value into the <select>
tag instead of using the if statement, plus keep it all together.
What's the proper syntax for this?
Thanks...
The code with comments pointing to the problem line:
render()
{
function buildQtyOptions(isAvailable, qty)
{
var opts = [];
if(!isAvailable)
{
opts.push(<td></td>);
return opts;
}
{/* Uncommenting this next line causes the error because the select is unclosed */}
{/* opts.push(<select>) */}
for (var i=1; i < 11; i++)
{
if(i === parseInt(qty))
{
opts.push(<option value={i} selected>{i}</option>);
}
else
{
opts.push(<option value={i}>{i}</option>);
}
}
{/* opts.push(</select>); */}
return opts;
}
{/* Other methods omitted here */}
return(
<tr>
<td>{getImage(this.props.available)}</td>
<td>{this.props.name}</td>
<td>
{/* I want to move this and the closing select into the buildQtyOptions method */}
{/*<select> */}
{ buildQtyOptions(this.props.available, this.props.years) }
{/*</select> */}
</td>
</tr>);