score:178
this requirement was removed in react version (16.0)]1, so now you are able to avoid that wrapper.
you can use react.fragment to render a list of elements without creating a parent node, official example:
render() {
return (
<react.fragment>
<childa />
<childb />
<childc />
</react.fragment>
);
}
more here: fragments
score:0
you won't be able to get rid of that div
element. react.render() needs to return one valid dom node.
score:0
score:0
there is workaround too. the below block code generates fragment without the need of react.fragment.
return [1,2,3].map(i=>{
if(i===1) return <div key={i}>first item</div>
if(i===2) return <div key={i}>second item</div>
return <div key={i}>third item</div>
})
score:1
this is still required, but react now make sure to create elements without creating an additional dom element.
the extra wrapping needed (normally with a parent div
) because reacts createelement
method require a type
parameter which is either a tag name string (such as 'div' or 'span'), a react component type (a class or a function)
. but this was before they introduce react fragment
.
refer this new api doc for createelement
react.createelement : create and return a new react element of the given type. the type argument can be either a tag name string (such as 'div' or 'span'), a react component type (a class or a function), or a react fragment type.
here is the official example, refer react.fragment.
render() {
return (
<react.fragment>
some text.
<h2>a heading</h2>
</react.fragment>
);
}
score:1
i know this question has been answered, you can of course use react.fragment which doesn't create a node but let's you group stuff like a div.
additionally if you want to have fun you can implement (and learn lots of things) a react mode that removes the extra div's and for this i really want to share a great video on how you can do it on the react code base itself.
https://www.youtube.com/watch?v=as41y_eynru
this is of course not something that you would do in practice but it's a good learning opportunity.
score:2
i created a component to wrap child components without a div. it's called a shadow wrapper: https://www.npmjs.com/package/react-shadow-wrapper
score:5
use [], instead of ()'s to wrap the entire return.
render: function() {
return[
<div classname="deadsimplecomponent__time">10:23:12</div >
<div classname="deadsimplecomponent__date">monday, 2 march 2015</div>
]
}
score:6
you can use:
render(){
return (
<react.fragment>
<div>some data</div>
<div>som other data</div>
</react.fragment>
)
}
for further details refer to this documentation.
score:69
update 2017-12-05: react v16.2.0 now fully supports rendering of fragments with improved support for returning multiple children from a components render method without specifying keys in children:
render() {
return (
<>
<childa />
<childb />
<childc />
</>
);
}
if you are using a react version prior to v16.2.0, it is also possible to use <react.fragment>...</react.fragment>
instead:
render() {
return (
<react.fragment>
<childa />
<childb />
<childc />
</react.fragment>
);
}
original:
react v16.0 introduced returning an array of elements in render method without wrapping it in a div: https://reactjs.org/blog/2017/09/26/react-v16.0.html
render() {
// no need to wrap list items in an extra element!
return [
// don't forget the keys :)
<li key="a">first item</li>,
<li key="b">second item</li>,
<li key="c">third item</li>,
];
}
at the moment, a key is required for each element to avoid the key warning but this could be changed in future releases:
in the future, we’ll likely add a special fragment syntax to jsx that doesn’t require keys.
Source: stackoverflow.com
Related Query
- How to avoid extra wrapping <div> in React?
- React-Router: How to avoid wrapping <Route /> elements in div
- How to avoid extra renders in my component to use react hooks
- React js avoid wrapping div
- How do you avoid extra React component renderings when using useEffect hook?
- How do I wrap a React component that returns multiple table rows and avoid the "<tr> cannot appear as a child of <div>" error?
- How to avoid React loading twice with Webpack when developing
- How do you center a div element in react w/out external css file
- How in React native destroy an component instance to avoid memory leak and release underlie views objects of native code?
- How to simulate mouse over event on a div using enzyme for testing a react application?
- how to hide and show a div in react
- How do I avoid unused setState functions? Can React useState be created without a setter?
- React Hooks - How to avoid redeclaring functions on every render
- How to avoid re-rendering the whole List instead of adding the new item to the DOM list in react JS?
- How can I avoid a TypeScript error with React useRef?
- How do I target DOM elements inside a react component, or should I avoid targeting DOM elements all together?
- how to hide or show a div if checkbox is selected in React JS
- React how to dynamically set div height to follow suit of full window height including scroll
- Adding styling to rails react component wrapping div
- How to avoid very long paths and use absolute paths within a Create React App project?
- How to avoid React re-renders when using reselect to compute derived state
- (Universal React + redux + react-router) How to avoid re-fetching route data on initial browser load?
- How to avoid re-render in react using useRef?
- Is it too expensive to add and remove event listeners on every call of my React custom hook? How to avoid it?
- How do I avoid re-rendering a connected React PureComponent due to mapDispatchToProps functions?
- How to check if a div is overflowing in react functional component
- Avoid rerender in React caused by objects literal : how to do with variables in the object?
- How to put a div inside a Switch with React Router?
- How to avoid unnecessary re-rendering of React Component
- How to avoid redeclaring props type in React constructor using Typescript
More Query from same tag
- React how to print not found in react js
- How to refactor to prevent component from losing its previous state (ReactJS)?
- I cant make rest calls with react
- GAE web.xml to fallback to React-Router
- How to filter a list using useEffect, useState, Typescript
- Handling Interval with onMouseEnter/onMouseLeave
- Looking for a better way to write this css grid (scss)
- How to display JS error from source map in DOM
- Keycloak-js 'init()' not executing in React app
- Using Async/Await In React for API Response
- Cannot display MAP from react-map-gl and Mapbox
- How to update component's state after redirect?
- User Context values doesnt change in child component
- how can remove array of object element without slice, splice
- Wordpress registerBlockType, data not fetched when saved
- How to configure @babel/plugin-proposal-class-properties in webpack.config.js?
- React jsx style tag not applied to tags returned by functions
- Insert image within imported React component (SOLVED!)
- Setting state array in react
- How to maintain state of child components when they are filtered through the parent component?
- How to fix : Cannot find module 'webpack/bin/config-yargs'
- Javascript reducer, how to stop && operator resetting values?
- Why does it direct me back to login page even user is authenticated?
- Console error while running react js sample web app
- 'Echarts' cannot assign both X-axis and Y-axis as type: value
- Reactjs and Typescript - Property 'select' does not exist on type 'EventTarget'
- How to style HTML element tags in MUI?
- React hook form handlesubmit doesn't work
- Pass param when using bind this with constructor in jsx
- Is there any way to hide certain PARAMETERS from URL ? I am using LINK. [REACTJS]