Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like React.Fragment in Vue?

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.

like image 597
RickyChan Avatar asked Dec 18 '19 09:12

RickyChan


People also ask

Does Vue have fragment?

Overview. In Vue 3, components now have official support for multi-root node components, i.e., fragments!

Can React components be used in Vue?

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!

How similar is Vue and React?

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.

Is Vue more powerful than React?

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.


4 Answers

Vue 2

Package Vue-fragment facilitates root-less components for vue 2.

Vue 3

Vue 3 have official support for multi-root node components (fragments) https://v3-migration.vuejs.org/new/fragments.html

like image 52
Jimish Fotariya Avatar answered Nov 03 '22 09:11

Jimish Fotariya


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>
like image 36
Roi Avatar answered Nov 03 '22 11:11

Roi


Apparently, Vue.js v3 now supports fragments out of the box: Fragments (Vue.js documentation)

like image 27
dakujem Avatar answered Nov 03 '22 11:11

dakujem


Vue v3

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>

like image 33
Raine Revere Avatar answered Nov 03 '22 09:11

Raine Revere