解析错误:必须将相邻的 JSX 元素包装在封闭标记中 [英] Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag
问题描述
我正在尝试设置我的 React.js
应用程序,以便它仅在我设置的变量为 true
时才呈现.
我的渲染函数的设置方式如下:
render: function() {var text = this.state.submitted ?'谢谢!期待在+电子邮件+"尽快跟进!: '输入您的电子邮件以请求抢先体验:';var style = this.state.submitted ?{"backgroundColor": "rgba(26, 188, 156, 0.4)"} : {};返回 (<div>如果(this.state.submitted==false){<input type="email" className="input_field" onChange={this._updateInputValue} ref="email" value={this.state.email}/><ReactCSSTransitionGroup transitionName="example" transitionAppear={true}><div className="button-row"><a href="#" className="button" onClick={this.saveAndContinue}>请求邀请</a>
</ReactCSSTransitionGroup>}
)},
基本上,这里重要的部分是 if(this.state.submitted==false)
部分(我希望这些 div
元素在提交变量时显示设置为 false
).
但是在运行这个时,我得到了问题中的错误:
<块引用>未捕获的错误:解析错误:第 38 行:必须将相邻的 JSX 元素包装在封闭标记中
这里有什么问题?我可以用什么来完成这项工作?
你应该把你的组件放在一个封闭的标签之间,这意味着:
//错了!返回 (<Comp1/><Comp2/>)
相反:
//正确返回 (<div><Comp1/><Comp2/>
)
Per Joe Clay 对 Fragments API的评论/p>
//更正确返回 (<React.Fragment><Comp1/><Comp2/></React.Fragment>)//简短的语法返回 (<><Comp1/><Comp2/></>)
I am trying to set up my React.js
app so that it only renders if a variable I have set is true
.
The way my render function is set up looks like:
render: function() {
var text = this.state.submitted ? 'Thank you! Expect a follow up at '+email+' soon!' : 'Enter your email to request early access:';
var style = this.state.submitted ? {"backgroundColor": "rgba(26, 188, 156, 0.4)"} : {};
return (
<div>
if(this.state.submitted==false)
{
<input type="email" className="input_field" onChange={this._updateInputValue} ref="email" value={this.state.email} />
<ReactCSSTransitionGroup transitionName="example" transitionAppear={true}>
<div className="button-row">
<a href="#" className="button" onClick={this.saveAndContinue}>Request Invite</a>
</div>
</ReactCSSTransitionGroup>
}
</div>
)
},
Basically, the important portion here is the if(this.state.submitted==false)
portion (I want these div
elements to show up when the submitted variable is set to false
).
But when running this, I get the error in the question:
Uncaught Error: Parse Error: Line 38: Adjacent JSX elements must be wrapped in an enclosing tag
What is the issue here? And what can I use to make this work?
You should put your component between an enclosing tag, Which means:
// WRONG!
return (
<Comp1 />
<Comp2 />
)
Instead:
// Correct
return (
<div>
<Comp1 />
<Comp2 />
</div>
)
Edit: Per Joe Clay's comment about the Fragments API
// More Correct
return (
<React.Fragment>
<Comp1 />
<Comp2 />
</React.Fragment>
)
// Short syntax
return (
<>
<Comp1 />
<Comp2 />
</>
)
这篇关于解析错误:必须将相邻的 JSX 元素包装在封闭标记中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!