In React I can do something like this:
// parent component
export default (){
return (
<div>
<div>1</div>
<ChildComponent />
<div>5</div>
</div>
);
}
// child component
export default (){
return (
<React.Fragment>
<div>2</div>
<div>3</div>
<div>4</div>
</React.Fragment>
);
};
// compiled html tags in browser .
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
But in Vue , when I've done the same thing , something went difference .
// parent component
<template>
<div>
<div>1</div>
<child-component />
<div>5</div>
</div>
</template>
<script>
import childComponent from 'path/to/childComponent';
export default {
components: { childComponent }
}
</script>
-------------------------------------------------------------
// child component
<template>
<div id='have_to_write_in_vue'>
<div>2</div>
<div>3</div>
<div>4</div>
<div>
</template>
// compiled html tags in browser .
<div>1</div>
<div id='have_to_write_in_vue'>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div>5</div>
The difference is that 'DIV' tags are not at same level in Vue . How can I handle this ?
I'm asking this is because of something went wrong when useless nesting appearing.
Overview. In Vue 3, components now have official support for multi-root node components, i.e., fragments!
The preferred way to use React inside of a Vue app is to use a Vue plugin. Now, use your React components like you would normally use your Vue components!
React and Vue are both lightweight, possess component-based architecture, and expose lifecycle methods. Their performance is fairly similar so those differences are too negligible to discuss. Both technologies work with any existing web application, even if it's not a Single Page Application.
Overall, Vue offers scaling projects and higher performance speed, but React. js has a better ecosystem, more templates, and additional tools. This is why teams on smaller projects where speed is a decisive factor, often opt for Vue, whereas React fits better to complex web platforms.
Package Vue-fragment facilitates root-less components for vue 2.
Vue 3 have official support for multi-root node components (fragments) https://v3-migration.vuejs.org/new/fragments.html
The lack of a first party Fragment component in Vue drives me nuts!
I had looked at the the vue-fragment
npm package, but it made me nervous at how much they were doing and felt there had to be a better way.
I came up with something extremely lightweight... It works because functional components are allowed to return multiple root nodes.
This doesn't work 100% like Reacts, however. In React, you can actually use a React.Fragment
as the root node in your app, for example. This seems to only work in Vue when you're already within a component (ie: it can't be the root node of your base component).
const Fragment = {
functional: true,
render: (h, ctx) => ctx.children
}
to use it...
<Fragment>
<div>Div 1</div>
<div>Div 2</div>
</Fragment>
Super useful...
{someCondition && (
<Fragment>
<div>yay i can use multiple nodes</div>
<div>:)</div>
</Fragment>
)}
<div>
Some inline text {condition ? (
<Fragment>now I can use <strong>html</strong> and {variables}</Fragment>
) : (
<Fragment>So many possibilities</Fragment>
)}
</div>
Apparently, Vue.js v3 now supports fragments out of the box: Fragments (Vue.js documentation)
This is a simple adaptation of @Roi's answer for Vue v3.
As @dakujem mentions, Vue v3 supports multi-root components out-of-the-box. However it does not support deeply nested fragments.
UPDATE: You can nest <template>
!
<template>
<div>
<ul>
<li>A</li>
<li>B</li>
<template>
<li>C</li>
<li>D</li>
</template>
</ul>
</div>
</template>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With