javascript - 编译反应失败
问题描述
我很确定我只是没有正确的语法,但似乎没有弄清楚正确的语法。这是我的代码(它在 a 内部,return()
并且 returnrender(){}
和 html 一起在 a 内部。
<div className="questions">
Now let's add some questions... <br />
{// This is where we loop through our questions to
// add them to the DOM.
this.state.questions.map(question => {
return (
<div>
{question}
{
for (var i = 0; i < 4; i++) {
answers.push(
<input
type="text"
onChange={this.handleChange}
name={uuid()}
/>
);
}
}
</div>
);
})
</div>
我对此很陌生,所以任何帮助都会非常有帮助,谢谢!
解决方案
您正在推送到answers
JSX 内部的一个数组,但从不使用它。您可以改为在 return 语句之前推送到数组,然后在 JSX 中使用它。
this.state.questions.map(question => {
const answers = [];
for (var i = 0; i < 4; i++) {
answers.push(
<input type="text" onChange={this.handleChange} />
);
}
return (
<div>
{question}
{answers}
</div>
);
});
或者,您可以使用Array.from
:
<div className="questions">
Now let's add some questions... <br />
{this.state.questions.map(question => {
return (
<div>
{question}
{Array.from({ length: 4 }, () => (
<input type="text" onChange={this.handleChange} />
))}
</div>
);
})}
</div>
推荐阅读
- django - 如何验证 forms.py 中的旧用户密码 - Django
- trusted-web-activity - 从 Chromebook 运行 TWA 只会在浏览器中打开 URL,而不是独立窗口
- wso2 - EnrichMediator:无法添加子项,因为目标不是 JSON 数组
- angular - 具有 mat-autocomplete 和 input 组合的 Angular mat-chips,在从列表中选择时添加一个额外的芯片
- django - Gunicorn/Nginx 上的 Django - Stripe Webhooks 总是获得 400
- azure - Azure AD B2c Xamarin Form 从不以静默方式登录
- c# - 在我自己的代码中使用事件处理程序的代码
- python - 带 LCD1602 显示屏的 MicroPython
- ios - SwiftUI 如何知道将哪个 environmentObject 分配给哪个变量
- python - 如何在 Python 中将 3 维数组(在本例中为滤波器组)与 2 维图像(单色)进行卷积?