javascript - React JSX 表达式在添加第二个时必须有父元素错误
问题描述
当我尝试添加另一个反应时返回错误JSX Expressions must have one parent element
。我不明白为什么这是因为它有一个父元素。
return (
<div className="loginButton">
<header className="loginButton">
<button className='discordLogin' id='login'
href="link-here"></button>
</header>
</div>
<div className="App">
<header className="App-header">
<img key={speed} src={logo} style={animationStyle} className="App-logo-circle" id='spinnerLogo'
alt="Spinning logo" />
<p>Hello, and welcome to the begining of the Swiss Plus Website. <strong>We hope you enjoy your stay</strong>
</p>
<button className='App-button' id='fastLogoButton' onClick={faster}>Increase Spin Speed!</button>
<button className='App-button' id='slowLogoButton' onClick={slower}>Decrease Spin Speed!</button>
</header>
</div>
);
PS。错误发生在返回的 ) 处。
解决方案
您有此错误,因为您同时返回两个元素。您的两个 div 都需要包装在父元素中。
你可以使用React.Fragment来做到这一点。如文档中所述(https://reactjs.org/docs/react-api.html#reactfragment)
React.Fragment 组件允许您在 render() 方法中返回多个元素,而无需创建额外的 DOM 元素
return(
<React.Fragment>
<div className="loginButton">
<header className="loginButton">
<button className='discordLogin' id='login'
href="link-here"></button>
</header>
</div>
<div className="App">
<header className="App-header">
<img key={speed} src={logo} style={animationStyle} className="App-logo-circle"
id='spinnerLogo'
alt="Spinning logo"
/>
<p>Hello, and welcome to the begining of the Swiss Plus Website. <strong>We hope
you enjoy your stay</strong>
</p>
<button className='App-button' id='fastLogoButton' onClick={faster}>
Increase Spin Speed!
</button>
<button className='App-button' id='slowLogoButton' onClick={slower}>
Decrease SpinSpeed!
</button>
</header>
</div>
</React.Fragment>
);
推荐阅读
- spark-submit - 如何使用多个 CPU 进行火花提交
- python - Django 2.2 静态文件在生产中不起作用(DEBUG False)
- c++ - 我的输出文件未显示解决方案
- pandas - 包含掩码操作
- r - r- flextable 在脚本和 RMarkdown Chunk 中运行,但 Knitr 失败
- python - 在 QLineEdit() 中输入值后,我想通过按搜索按钮仅显示 QTableWidget 中的行
- html - Flex,图片和滚动位置
- r - 如何获得混淆矩阵多类图像 Keras R
- laravel - laravel Auth::user() null > 会话文件不存在
- javascript - 如何发送由 javascript 上传的文件并在烧瓶应用程序中接收并将其作为发布请求发送到另一台服务器?