解析错误:相邻的JSX元素必须包装在一个封闭标签中

在尝试设置React应用时遇到错误:相邻的JSX元素必须包裹在一个封闭标签中。问题在于返回了多个同级JSX元素。解决方案包括使用Fragments、返回数组或包裹在一个父组件中。在React 16.2+版本中,可以使用<></>语法,而在早期版本中,可以将元素放入数组或添加父组件。

本文翻译自:Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag

I am trying to set up my React.js app so that it only renders if a variable I have set is true. 我试图设置我的React.js应用程序,以便仅在我设置的变量为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 divs to show up when the submitted variable is set to false). 基本上,这里的重要部分是if(this.state.submitted == false)部分(当提交的变量设置为false时,我希望这些div出现)。

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 未捕获的错误:解析错误:第38行:相邻的JSX元素必须包装在一个封闭的标记中

What is the issue here? 这是什么问题? And what can I use to make this work? 我可以用什么做这项工作?


#1楼

参考:https://stackoom.com/question/27GRN/解析错误-相邻的JSX元素必须包装在一个封闭标签中


#2楼

React element has to return only one element. React元素只能返回一个元素。 You'll have to wrap both of your tags with another element tag. 您必须将两个标签都包装在另一个元素标签上。

I can also see that your render function is not returning anything. 我还可以看到您的渲染函数未返回任何内容。 This is how your component should look like: 这是您的组件的外观:

var app = React.createClass({
    render () {
        /*React element can only return one element*/
        return (
             <div></div>
        )
    }
})

Also note that you can't use if statements inside of a returned element: 还要注意,您不能在返回的元素内使用if语句:

render: function() {
var text = this.state.submitted ? 'Thank you!  Expect a follow up at '+email+' soon!' : 'Enter your email to request early
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值