admin管理员组文章数量:1332339
I am trying to build a react app with bootstrap that will showcase the cards that's like C.C.C. as display as 3 in a row and there's 3 rows, but I get this error:
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...?
Here is the code:
import React from 'react';
import './App.css';
import "bootstrap/dist/css/bootstrap.min.css";
import { Card } from "react-bootstrap";
import { Button } from "react-bootstrap";
function App() {
return (
<Card>
<Card.Img variant="top"
src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead - in to
additional content.This content is a little bit longer.
</Card.Text>
</Card.Body>
</Card>
<Card>
<Card.Img variant="top" src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This card has supporting text below as a natural lead - in to additional
content. {' '}
</Card.Text>
</Card.Body>
</Card>
<Card>
<Card.Img variant="top"
src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>123</Card.Text>
</Card.Body>
</Card>
);
}
export default App;
I am trying to build a react app with bootstrap that will showcase the cards that's like C.C.C. as display as 3 in a row and there's 3 rows, but I get this error:
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...?
Here is the code:
import React from 'react';
import './App.css';
import "bootstrap/dist/css/bootstrap.min.css";
import { Card } from "react-bootstrap";
import { Button } from "react-bootstrap";
function App() {
return (
<Card>
<Card.Img variant="top"
src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead - in to
additional content.This content is a little bit longer.
</Card.Text>
</Card.Body>
</Card>
<Card>
<Card.Img variant="top" src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This card has supporting text below as a natural lead - in to additional
content. {' '}
</Card.Text>
</Card.Body>
</Card>
<Card>
<Card.Img variant="top"
src="holder.js/100px160" />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>123</Card.Text>
</Card.Body>
</Card>
);
}
export default App;
Share
Improve this question
edited Feb 24, 2020 at 22:26
Brian Thompson
14.4k4 gold badges27 silver badges49 bronze badges
asked Feb 24, 2020 at 22:12
jenniejennie
333 silver badges9 bronze badges
1
- 3 Does this answer your question? Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag – Brian Thompson Commented Feb 24, 2020 at 22:20
2 Answers
Reset to default 6As the error says, you have a syntax error. Your function (every function in JavaScript) can only return a single value. If you expand what the JSX syntax does, your function is currently doing something like this:
return (
React.createElement(Card, ...)
React.createElement(Card, ...)
React.createElement(Card, ...)
)
...which is not valid syntax. You need to either wrap those <Card/>
elements in a container element or do what the error suggests, using a React fragment:
return (
<>
<Card .../>
<Card .../>
<Card .../>
</>
);
A fragment has the benefit of being a single value while not adding the cost of an extraneous DOM element.
From the error message:
Adjacent JSX elements must be wrapped in an enclosing tag
This means that a ponent cannot return multiple JSX elements, only a single JSX element (which may have multiple children and descendants).
Currently, you have the basic structure:
return (
<Card>...</Card>
<Card>...</Card>
<Card>...</Card>
)
As the message goes on to state:
Did you want a JSX fragment <>...< />?
So you can fix this with:
return (
<>
<Card>...</Card>
<Card>...</Card>
<Card>...</Card>
</>
)
or
return (
<React.Fragment>
<Card>...</Card>
<Card>...</Card>
<Card>...</Card>
</React.Fragment>
)
本文标签:
版权声明:本文标题:javascript - React.js Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment & 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327181a2453955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论