score:-1
for rect-native developers. i encounter this error while renderingitem in flatlist. i had two text components. i was using them like below
renderitem = { ({item}) =>
<text style = {styles.item}>{item.key}</text>
<text style = {styles.item}>{item.user}</text>
}
but after i put these tow inside view components it worked for me.
renderitem = { ({item}) =>
<view style={styles.flatview}>
<text style = {styles.item}>{item.key}</text>
<text style = {styles.item}>{item.user}</text>
</view>
}
you might be using other components but putting them into view may be worked for you.
score:-1
i think the complication may also occur when trying to nest multiple divs within the return statement. you may wish to do this to ensure your components render as block elements.
here's an example of correctly rendering a couple of components, using multiple divs.
return (
<div>
<h1>data information</h1>
<div>
<button type="primary">create data</button>
</div>
</div>
)
score:0
import view and wrap in view
. wrapping in a div
did not work for me.
import { view } from 'react-native';
...
render() {
return (
<view>
<h1>foo</h1>
<h2>bar</h2>
</view>
);
}
score:0
invalid: not only child elements
render(){
return(
<h2>responsive form</h2>
<div>adjacent jsx elements must be wrapped in an enclosing tag</div>
<div classname="col-sm-4 offset-sm-4">
<form id="contact-form" onsubmit={this.handlesubmit.bind(this)} method="post">
<div classname="form-group">
<label for="name">name</label>
<input type="text" classname="form-control" id="name" />
</div>
<div classname="form-group">
<label for="exampleinputemail1">email address</label>
<input type="email" classname="form-control" id="email" aria-describedby="emailhelp" />
</div>
<div classname="form-group">
<label for="message">message</label>
<textarea classname="form-control" rows="5" id="message"></textarea>
</div>
<button type="submit" classname="btn btn-primary">submit</button>
</form>
</div>
)
}
valid: root element under child elements
render(){
return(
<div>
<h2>responsive form</h2>
<div>adjacent jsx elements must be wrapped in an enclosing tag</div>
<div classname="col-sm-4 offset-sm-4">
<form id="contact-form" onsubmit={this.handlesubmit.bind(this)} method="post">
<div classname="form-group">
<label for="name">name</label>
<input type="text" classname="form-control" id="name" />
</div>
<div classname="form-group">
<label for="exampleinputemail1">email address</label>
<input type="email" classname="form-control" id="email" aria-describedby="emailhelp" />
</div>
<div classname="form-group">
<label for="message">message</label>
<textarea classname="form-control" rows="5" id="message"></textarea>
</div>
<button type="submit" classname="btn btn-primary">submit</button>
</form>
</div>
</div>
)
}
score:0
just add
<>
// code ....
</>
score:1
react 16 gets your return as an array so it should be wrapped by one element like div.
wrong approach
render(){
return(
<input type="text" value="" onchange={this.handlechange} />
<button classname="btn btn-primary" onclick= {()=>this.addtodo(this.state.value)}>submit</button>
);
}
right approach (all elements in one div or other element you are using)
render(){
return(
<div>
<input type="text" value="" onchange={this.handlechange} />
<button classname="btn btn-primary" onclick={()=>this.addtodo(this.state.value)}>submit</button>
</div>
);
}
score:1
react components must wrapperd in single container,that may be any tag e.g. "< div>.. < / div>"
you can check render method of reactcsstransitiongroup
score:4
there is a rule in react that a jsx expression must have exactly one outermost element.
wrong
const para = (
<p></p>
<p></p>
);
correct
const para = (
<div>
<p></p>
<p></p>
</div>
);
score:7
if you do not wrap your component then you can write it as mentioned below method.
instead of:
return(
<comp1 />
<comp2 />
);
you can write this:
return[(
<comp1 />
),
(
<comp2 />
) ];
score:7
it's very simple we can use a parent element div to wrap all the element or we can use the higher order component( hoc's ) concept i.e very useful for react js applications
render() {
return (
<div>
<div>foo</div>
<div>bar</div>
</div>
);
}
or another best way is hoc its very simple not very complicated just add a file hoc.js in your project and simply add these codes
const aux = (props) => props.children;
export default aux;
now import hoc.js file where you want to use, now instead of wrapping with div element we can wrap with hoc.
import react, { component } from 'react';
import hoc from '../../../hoc';
render() {
return (
<hoc>
<div>foo</div>
<div>bar</div>
</hoc>
);
}
score:24
react 16.0.0 we can return multiple components from render as an array.
return ([
<comp1 />,
<comp2 />
]);
react 16.2.0 > we can return multiple components from render in a fragment tag. fragment
return (
<react.fragment>
<comp1 />
<comp2 />
</react.fragment>);
react 16.2.0 > you can use this shorthand syntax. (some older tooling versions don’t support it so you might want to explicitly write <fragment>
until the tooling catches up.)
return (
<>
<comp1 />
<comp2 />
</>)
score:50
the problem
parse error: adjacent jsx elements must be wrapped in an enclosing tag
this means that you are trying to return multiple sibling jsx elements in an incorrect manner. remember that you are not writing html, but jsx! your code is transpiled from jsx into javascript. for example:
render() {
return (<p>foo bar</p>);
}
will be transpiled into:
render() {
return react.createelement("p", null, "foo bar");
}
unless you are new to programming in general, you already know that functions/methods (of any language) take any number of parameters but always only return one value. given that, you can probably see that a problem arises when trying to return multiple sibling components based on how createelement()
works; it only takes parameters for one element and returns that. hence we cannot return multiple elements from one function call.
so if you've ever wondered why this works...
render() {
return (
<div>
<p>foo</p>
<p>bar</p>
<p>baz</p>
</div>
);
}
but not this...
render() {
return (
<p>foo</p>
<p>bar</p>
<p>baz</p>
);
}
it's because in the first snippet, both <p>
-elements are part of children
of the <div>
-element. when they are part of children
then we can express an unlimited number of sibling elements. take a look how this would transpile:
render() {
return react.createelement(
"div",
null,
react.createelement("p", null, "foo"),
react.createelement("p", null, "bar"),
react.createelement("p", null, "baz"),
);
}
solutions
depending on which version of react you are running, you do have a few options to address this:
use fragments (react v16.2+ only!)
as of react v16.2, react has support for fragments which is a node-less component that returns its children directly.
returning the children in an array (see below) has some drawbacks:
- children in an array must be separated by commas.
- children in an array must have a key to prevent react’s key warning.
- strings must be wrapped in quotes.
these are eliminated from the use of fragments. here's an example of children wrapped in a fragment:
render() { return ( <> <childa /> <childb /> <childc /> </> ); }
which de-sugars into:
render() { return ( <react.fragment> <childa /> <childb /> <childc /> </react.fragment> ); }
note that the first snippet requires babel v7.0 or above.
return an array (react v16.0+ only!)
as of react v16, react components can return arrays. this is unlike earlier versions of react where you were forced to wrap all sibling components in a parent component.
in other words, you can now do:
render() { return [<p key={0}>foo</p>, <p key={1}>bar</p>]; }
this transpiles into:
return [react.createelement("p", {key: 0}, "foo"), react.createelement("p", {key: 1}, "bar")];
note that the above returns an array. arrays are valid react elements since react version 16 and later. for earlier versions of react, arrays are not valid return objects!
also note that the following is invalid (you must return an array):
render() { return (<p>foo</p> <p>bar</p>); }
wrap the elements in a parent element
the other solution involves creating a parent component which wraps the sibling components in its
children
. this is by far the most common way to address this issue, and works in all versions of react.render() { return ( <div> <h1>foo</h1> <h2>bar</h2> </div> ); }
note: take a look again at the top of this answer for more details and how this transpiles.
score:102
if you don't want to wrap it in another div as other answers have suggested, you can also wrap it in an array and it will work.
// wrong!
return (
<comp1 />
<comp2 />
)
it can be written as:
// correct!
return (
[<comp1 />,
<comp2 />]
)
please note that the above will generate a warning: warning: each child in an array or iterator should have a unique "key" prop. check the render method of 'yourcomponent'.
this can be fixed by adding a key
attribute to the components, if manually adding these add it like:
return (
[<comp1 key="0" />,
<comp2 key="1" />]
)
here is some more information on keys:composition vs inheritance
score:121
react element has to return only one element. 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:
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)"} : {};
if(this.state.submitted==false) {
return <yourjsx />
} else {
return <yourotherjsx />
}
},
score:286
it is late to answer this question but i thought it will add to the explanation.
it is happening because any where in your code you are returning two elements simultaneously.
e.g
return(
<div id="div1"></div>
<div id="div1"></div>
)
it should be wrapped in a parent element. e.g
return(
<div id="parent">
<div id="div1"></div>
<div id="div1"></div>
</div>
)
more detailed explanation
your below jsx
code get transformed
class app extends react.component {
render(){
return (
<div>
<h1>welcome to react</h1>
</div>
);
}
}
into this
_createclass(app, [{
key: 'render',
value: function render() {
return react.createelement(
'div',
null,
react.createelement(
'h1',
null,
'welcome to react'
)
);
}
}]);
but if you do this
class app extends react.component {
render(){
return (
<h1>welcome to react</h1>
<div>hi</div>
);
}
}
this gets converted into this (just for illustration purpose, actually you will get error : adjacent jsx elements must be wrapped in an enclosing tag
)
_createclass(app, [{
key: 'render',
value: function render() {
return react.createelement(
'div',
null,
'hi'
);
return react.createelement(
'h1',
null,
'welcome to react'
)
}
}]);
in the above code you can see that you are trying to return twice from a method call, which is obviously wrong.
edit- latest changes in react 16 and own-wards:
if you do not want to add extra div to wrap around and want to return more than one child components you can go with react.fragments
.
react.fragments
(<react.fragments>
)are little bit faster and has less memory usage (no need to create an extra dom node, less cluttered dom tree).
e.g (in react 16.2.0)
render() {
return (
<>
react fragments.
<h2>a heading</h2>
more react fragments.
<h2>another heading</h2>
even more react fragments.
</>
);
}
or
render() {
return (
<react.fragments>
react fragments.
<h2>a heading</h2>
more react fragments.
<h2>another heading</h2>
even more react fragments.
</react.fragments>
);
}
or
render() {
return [
"some text.",
<h2 key="heading-1">a heading</h2>,
"more text.",
<h2 key="heading-2">another heading</h2>,
"even more text."
];
}
score:761
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 />
</>
)
Source: stackoverflow.com
Related Query
- Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag
- Wepack error : Adjacent JSX elements must be wrapped in an enclosing tag
- Adjacent JSX elements must be wrapped in an enclosing tag - JSX tag already wrapped but keep omitting the error
- ReactJS switch case error Adjacent JSX elements must be wrapped in an enclosing tag
- Adjacent JSX elements must be wrapped in an enclosing tag with line break tag
- Error: Adjacent JSX elements must be wrapped in an enclosing tag
- how to resolve Adjacent JSX elements must be wrapped in an enclosing tag issue in React
- ReactJS - Adjacent JSX elements must be wrapped in an enclosing tag Error. When trying to include two <tr> tag
- Adjacent JSX elements must be wrapped in an enclosing tag error, when it wrapped in a closing tag
- Adjacent JSX elements must be wrapped in enclosing tag
- Adjacent JSX elements must be wrapped in an enclosing tag - gatsby.js
- React: Adjacent JSX elements must be wrapped in an enclosing tag
- Adjacent JSX elements must be wrapped in an enclosing tag with bootstrap columns
- ReactJS Adjacent JSX elements must be wrapped in an enclosing tag
- parsing error with braces in react: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
- semantic-ui-react Syntax error: Adjacent JSX elements must be wrapped in an enclosing tag
- Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag
- Adjacent JSX Elements must be wrapped in an enclosing tag reactjs
- React.js Error "Adjacent JSX elements must be wrapped in an enclosing tag"
- React.js Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
- Parse Error:Adjacent JSX elements must be wrapped in an enclosing tag(React.js )
- Getting 'Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag' even when I use React.Fragment
- Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment -ReactJS
- JSX mapping element within mapping element, Adjacent JSX elements must be wrapping in enclosing tags
- jsx must wrap with an enclosing tag error
- must be wrapped in an enclosing tag
- Trying to debug Adjacent JSX elements error
- Tag Error: React JSX Style Tag Error on Render
- React Syntax Error : Expected corresponding JSX closing tag for <img>
- Typescript error when trying to map over Enum keys to produce JSX elements
More Query from same tag
- why state returned from reducer gets wrapped in an object
- Event handler not picking up correct state value
- How to solve the issue of a text cursor in a text input being automatically placed at the end of the input after a typed character has been inserted?
- React Component with SubComponent - hot reload does not work
- Re-exporting MUI components from React component library
- componentDidMount and React.lazy
- React Native Event
- How to define React Navigation navigationOptions parameter in Typescript
- is it possible to make AG Grid custom cell editor with <input type="file"/>
- How to import scss file as variable in react with typescript
- React +TypeScript - component that renders a dynamic element issue
- React - SetState before rendering
- How to get Component by dynamic testid using React Testing Library
- React - component state not being set
- Reactjs - retrieve attributes from event.target
- Get a bunch of errors when trying to run "npm start" after running "create-react-app my-app"
- How to update useState hook with Object which gets an array? with typescript
- Use multiple 'useContext' in one React component
- Adjacent component interaction in Thermite
- React state is array of objects which are react elements
- Original list doesn't show up after a search is made in planets list
- undefined errors object in react-hook-form
- How to send data from react and use the request body in express, using Axios?
- Error using axios interceptors and responses
- Why does JavaScript acts different with Sync/Async in React and node?
- How can I render in my column names conditionally AFTER the list is pulled from my backend?
- What does "_" .(insert function) do in JavaScript? (React example)
- I need solve a problem with websockets on golang server
- React - nested objects and useState is mutating original state
- React - set direction arrow to table headings