If you're working on a component and already have a root div with multiple nodes from sub-render functions, it's simple to handle.
In React, we often divide render functions and the same approach can be taken here as well.
Here is a possible solution:
Below is the code snippet:
render(h) {
return (
<div>
{this.isConfirm ? this.renderF1(h) : this.renderF2(h)}
</div>
);
},
methods: {
//eslint-disable-next-line
renderF1: function(h){
return [
<div></div>,
<div class='anotherDiv'></div>
]
},
//eslint-disable-next-line
renderF2: function(h){
return [
<span></span>,
<div class='secondDiv'></div>
]
}
}
An Explanation:
The first step involves returning multiple root node elements in an array like this:
return [node, node];
Then, pass the h
function as an argument to the smaller render functions from your main render
function to avoid Vue CLI errors related to function h
.
Once these adjustments are made, the code should run without issues.
Additional Note: If you are using eslint
, consider adding this line at the top of each render function to prevent compilation errors:
//eslint-disable-next-line
Also, if you are new to JSX in Vuejs, you can utilize the official Babel plugin package for assistance.
When dealing with component hierarchies, ensure that you structure them in such a way that avoids errors when using arrays as the root element.
For instance, attempting something similar to:
render(h) {
return [
<div>
{this.isConfirm ? this.renderF1(h) : this.renderF2(h)}
</div>
];
},
This will prompt an error message like:
Multiple root nodes returned from render function. Render function should return a single root node.