【发布时间】:2017-10-01 15:28:11
【问题描述】:
在一个 React 类组件中,我正在编写一个构建 <select> 下拉列表的方法,而 Babel 给出了一个错误。该错误是由第一个<select> 标签未关闭引起的。我无法找出正确的 react-jsx 语法。
在下面的代码中,我试图将创建<select> 的代码封装到组件类中的另一个方法中。如果我有该函数,只需构建选项并将<select> 和</select> 放在该函数周围(在渲染方法中)它可以工作,但我想将所选值放入<select> 标签而不是使用if 语句,再加上将它们放在一起。
正确的语法是什么?
谢谢...
cmets指向问题行的代码:
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>);
【问题讨论】: