The Vue render function is displaying the [object Object] result

I am attempting to show an icon when a certain condition is met, using a theme icon

Script Code
<script setup>
import { h } from 'vue'
const props = defineProps({
  itemDetail: Object
});

const passedIcon = h('i', {class: 'bx bxs-check-circle'});
const failedIcon = h('i', {class: 'bx bxs-cross-circle'});
</script>
Template Code
<template>
 <tr>
  <td>Zip check</td>
  <td>
   {{ props.itemDetail.status == 'pass' ? 'Passed ' + passedIcon : 'Failed ' + failedIcon }}
  </td>
 </tr>
</template>

However, the output shows:

Passed [object Object]

or 

Failed [object Object]

I experimented with the h() render function and it worked fine, but how can I directly call it as HTML within the template?

Answer №1

You have the option to include the icon class names directly inside the template by using conditional rendering :

<script setup>
const props = defineProps({
  itemDetail: Object
});

</script>
<template>
 <tr>
  <td>Zip check</td>
  <td>
    <template v-if="itemDetail.status == 'pass'">
      Passed <i class="bx bxs-check-circle"/>
    </template>
    <template v-else>
       Failed <i class="bx bxs-cross-circle"/>
    </template>
  </td>
 </tr>
</template>

Another approach is to utilize the built-in component component along with its prop is bound to the icon based on the condition :

<script setup>
import { h, computed } from 'vue'
const props = defineProps({
  itemDetail: Object
});

const passedIcon = h('i', { class: 'bx bxs-check-circle' }, );
const failedIcon = h('i', { class: 'bx bxs-cross-circle' }, );
const icon = computed(() => {
  return props.itemDetail.status == 'pass' ? passedIcon : failedIcon
})
</script>

<template>
  <tr>
    <td>Zip check</td>
    <td>
      {{ itemDetail.status == 'pass' ? 'Passed ' : 'Failed ' }}
      <component :is="icon" />
    </td>
  </tr>
</template>

DEMO

You can also streamline and improve code readability by reorganizing the vnode created by h:

const passedContent = h('span',{},['Passed',h('i', {class: 'bx bxs-check-circle'})]);
...

Answer №2

Vue Components Playground

Transform your functional components to functions - they are labeled as functional for a reason:

<script setup>
import { h } from 'vue'
const props = defineProps({
  itemDetail: {type: Object, default: {}}
});

const passedIcon = () => h('i', {class: 'bx bxs-check-circle'});
const failedIcon = () => h('i', {class: 'bx bxs-cross-circle'});
</script>

<template>
 <tr>
  <td>Zip verification</td>
  <td>
  <template v-if="itemDetail.status == 'pass'">Passed <passed-icon /></template>
   <template v-else>Failed <failed-icon /></template>
  </td>
 </tr>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animation displayed on page load before running

Is there a way to ensure that my animation runs smoothly without any lag or jankiness, either while the page is loading or immediately after loading? I have attempted to preload the images using a preload tag in the header of my HTML, but unfortunately, t ...

Using JavaScript and the Firefox browser, learn how to easily highlight an element with Selenium-WebDriver

I am struggling with creating a valid function to highlight specific elements on a webpage. As a beginner in coding, I suspect that the issue may either be related to my environment setup or a lack of knowledge about JavaScript/Selenium features. I am wri ...

Having trouble with Semantic UI Modal onShow / onVisible functionality?

Seeking assistance with resizing an embedded google map in a Semantic UI modal after it is shown. After numerous attempts, I have narrowed down the issue to receiving callbacks when the modal becomes visible. Unfortunately, the onShow or onVisible functio ...

Retrieving real-time data in VueJS using vuex framework

I run a shop with various components that rely on common data: export default new Vuex.Store({ state: { _invoices: [], }, mutations: { setInvoices: (state, invoices) => (state._invoices = invoices), }, actions: { ...

What is the correct way to transform an Error object to a string in Node.js?

Every time I input a duplicate entry in mysql, this error pops up. { [Error: ER_DUP_ENTRY: Duplicate entry '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35465458455950755258545c591b-565a58">[email protected]< ...

Creating environment-agnostic builds with the Vue webpack template

When building my Vue projects using a build server, I rely on the npm run build command provided by the Vue 2 template. This template allows me to access environment-specific data configured in files within the config directory, such as prod.env.js. The ac ...

Navigate through a collection of Facebook profiles

Currently, I am working on creating a Google Chrome extension that will place a button on the Facebook profile cover. Using the content script provided in the Chrome API documentation, I have successfully inserted the button when refreshing the page or ope ...

"Enhance Your Communication: Utilize setTimeout in Ajax

Hey there, I could really use some help with the setTimeout function in my code. No matter what I try, it just doesn't seem to work. I'm currently working on a chat system where I need to send and receive messages (testing by opening 2 browser ...

Unraveling encoded characters in URLs using Node.js

When I try to send a cookie back using res.cookie(JSON.stringify({bar: "baz"}), the value that I get in return is %7B%22bar%22%3A%22baz%22%7D. How can I decode this in a Javascript setting like node.js? ...

The step-by-step guide on displaying API choices in an Autocomplete feature and keeping them up

Having trouble with updating autocomplete options. An error message pops up in the console log when I try to deselect a tag or select a new one: MUI: The value provided to Autocomplete is invalid. None of the options match with [{"catName":{&qu ...

Error Unhandled in Node.js Application

I have encountered an issue in my NodeJS application where I have unhandled code in the data layer connecting to the database. I deliberately generate an error in the code but do not catch it. Here is an example: AdminRoleData.prototype.getRoleByRoleId = ...

How can we determine if a Node.js application will remain operational?

How is the longevity of a Node.js app determined? For example, consider this code snippet: console.log('Hello World'); In this case, the phrase will be printed and the app will exit immediately. However, with a web server that is actively lis ...

Different results are being obtained when destructuring props in a component

Just diving into the world of React and trying to grasp destructuring. I've been doing some reading on it, but I'm currently stuck. When I try to destructure like this function MList({action}) { // const data = [action];}, I only get 'camera ...

I am encountering an issue where the nested loop in Angular TypeScript is failing to return

I am facing an issue with my nested loop inside a function. The problem is that it is only returning the default value of false, instead of the value calculated within the loop. Can someone please point out what I might be doing incorrectly? Provided belo ...

Experiencing problems with the calling convention in JavaScript

Here is a snapshot of the code provided: If the function testFields at Line:3 returns false, then the program correctly moves to Line:21 and returns the value as false. However, if testFields returns true, the program proceeds to Line:4, but instead of ex ...

Is it possible to launch a Nextjs app on Vercel for production purposes? How well does it handle high volumes of traffic?

As a newcomer to Nextjs, I am looking to deploy my app to production. I'm curious about whether Vercel can effectively handle heavy traffic on the site. Should I consider utilizing platforms such as AWS or GCP for deployment instead? Any advice would ...

Edit HTML and CSS in live mode while also running JavaScript for instant enhancement

Recently, I created a script that allows me to build a web page in real-time. I can input HTML and CSS codes without any issues, but unfortunately, I am unable to execute JavaScript due to the limitations of the string-based editor I am using. Can anyone o ...

Strategies for transferring class attribute to child component element

Is there a way to pass the class attribute from parent to child component element in React? Check out this example: https://codesandbox.io/s/pedantic-shamir-1wuuv I want to add a class to the Component "Input Field" https://i.sstatic.net/yphxk.png My go ...

Issue in IE with click event not firing from parent page

Currently, I am facing an issue with a filter on one of my website's pages that is designed to display various products. My goal is to set it up so that when a button from an external page is clicked, the user will be redirected to the product page wh ...

Gather numerical values and transform them into dates using AngularJS

My Angularjs project involves 3 scopes for year, month, and day which are retrieved from 3 input boxes. I need to combine them into a date, but the current code inserts the date with an additional 22:00:00 like 2019-06-01 22:00:00.000. How can I insert the ...