What is the proper way to invoke render functions using Vue 3 composition API?

During my time with Vue 2, I would typically call render() in this manner:

export default {
    mounted(){
        ...
    },
    render(){
        ...
    },
    methods(){
        ...
    }
}

Now that I'm exploring Vue 3 and the composition API, I attempted to replicate the same approach. Here's what I experimented with:

export default {
    ...
    setup(props, context){
        ...
        const create_canvas = (h, id, props) => {
            _id.value = id
            _attrs.value = props.attrs
            return () => h('div', {
                class: `trading-vue-${id}`,
                style: {
                    left: props.position.x + 'px',
                    top: props.position.y + 'px',
                    position: 'absolute',
                }
            }, [
                h('canvas', Object.assign({
                    id: `${props.tv_id}-${id}-canvas`,
                    onmousemove: e => renderer.mousemove(e),
                    onmouseout: e => renderer.mouseout(e),
                    onmouseup: e => renderer.mouseup(e),
                    onmousedown: e => renderer.mousedown(e),
                    ref: 'canvas',
                    style: props.style,
                }, props.attrs))
            ].concat(props.hs || []))
        };

        function render() {
            const id = props.grid_id
            const layout = props.layout.grids[id]
            return () => create_canvas(h, `grid-${id}`, {
                position: {
                    x: 0,
                    y: layout.offset || 0
                },
                attrs: {
                    width: layout.width,
                    height: layout.height,
                    overflow: 'hidden'
                },
                style: {
                    backgroundColor: props.colors.back
                },
                hs: [
                    h(Crosshair, Object.assign(
                        common_props(),
                        layer_events
                    )),
                    h(KeyboardListener, keyboard_events),
                    h(UxLayer, {
                        id,
                        tv_id: props.tv_id,
                        uxs: uxs.value,
                        colors: props.colors,
                        config: props.config,
                        updater: Math.random(),
                        onCustomEvent: emit_ux_event
                    })
                ].concat(get_overlays(h))
            })
        };

        render()
    }
}

However, it seems like this code doesn't display anything in my template. I suspect that I might not be utilizing the render function correctly. Can someone assist me in understanding how to properly employ it?

Answer №1

Based on my understanding, the function h() is a concise way to generate vnodes and requires 3 parameters.

h(
    tag name,
    props/attributes,
    array of children
)

From what I gather, within the create_canvas function, you are attempting to create a div element with specified class and inline styles as attributes/props, and then adding a canvas element as a child to this div vnode. Therefore, instead of directly returning the vNode from setup(), it would be more appropriate to return a render function that in turn returns a vNode.

export default {
    props: {
      // props will be added here 
    },
    setup(props) {
        // render() { h(...) } ❌
           return () => {
               h('div', {
                class: `trading-vue-${id}`,
                style: {
                    left: props.position.x + 'px',
                    top: props.position.y + 'px',
                    position: 'absolute',
                }
            }, [
                h('canvas', Object.assign({
                    id: `${props.tv_id}-${id}-canvas`,
                    onmousemove: e => renderer.mousemove(e),
                    onmouseout: e => renderer.mouseout(e),
                    onmouseup: e => renderer.mouseup(e),
                    onmousedown: e => renderer.mousedown(e),
                    ref: 'canvas',
                    style: props.style,
                }, props.attrs))
            ].concat(props.hs || []))
       } ✅
    }
}

Answer №2

Instead of explicitly calling the render function, you simply declare it in Vue (and Vue will call it during rendering).

With the Composition API, all you need to do is return the render function from the setup method.

import { ref, h } from 'vue'

export default {
  props: {
    /* ... */
  },
  setup(props) {
    const count = ref(1)

    // return the render function
    return () => h('div', props.msg + count.value)
  }
}

When applying this concept to your own code, remember that the last line of the setup should be a return render() instead of just render() because the actual "render" function is returned by the render() function itself.


In JavaScript, functions can be treated as data - you can store and return them. The function is not immediately executed when stored or returned within another function, but rather created for later use. The caller of the "factory" function (in this case, the Vue framework) can save the reference to the returned function and decide when to execute it.

The Vue Composition API onMounted hook operates in a similar way. You pass a newly created function to onMounted(), which then stores the reference for Vue to call at a later point.

It's important to understand that the order of execution inside the setup() method does not matter since Vue controls when to call these functions. Vue will likely ensure that the render function is called at least once before any functions passed into onMounted() are executed (as the component must be rendered before being mounted).

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

Fixing the hydration error in Next 13 can be challenging, especially when it occurs outside of a Suspense boundary

Encountering an issue while working with Next.js 13: Error: Hydration failed because the initial UI does not match what was rendered on the server. Warning: Expected server HTML to contain a matching <div> in <html>. Every time I attempt to r ...

Set up a mouseover function for a <datalist> tag

Currently, I am delving into the world of javascript/jquery and I have set up an input field with a datalist. However, I am encountering a slight hurdle - I am unable to trigger an event when hovering over the datalist once it appears. Although I have man ...

Ways to eliminate reliance on jQuery

While researching how to integrate Plotly with Vue, I came across this example: https://codepen.io/rhamner/pen/MXgWqJ The example meets my requirements, but it relies on jquery js. I'd like to eliminate the dependency on the jquery js file. I attem ...

Make sure to leave a space after a period in a sentence, but do

My question is about fixing spacing issues in text, specifically sentences that lack spaces after a dot. For example: See also vadding.Constructions on this term abound. I also have URLs within the text, such as: See also vadding.Constructions on th ...

Utilize JQuery to implement fading effects for clicked elements in a webpage

I've been using a rollover JavaScript plugin to create smooth transitional effects when users hover over clickable page elements. Everything was going well until I decided to switch to making ajax calls instead of page loads for dynamic content. The p ...

Retrieve an image file from Laravel API within Vue Cli 3

I have set up a Laravel API as my backend and Vue Cli 3 for the frontend of my project. I am currently facing an issue trying to access images from storage/app/avatars in Laravel from my Vue front end. To tackle this, I ran the php artisan storage:link com ...

Filter and search JSON data using React Native

Recently I have started learning about react-native and I am currently utilizing it for my school assignment. Prior to this, I was working with ionic. My current task involves filtering data that is stored in JSON format. I'm curious to know if react ...

Struggling to retrieve data from AJAX POST request [Revised Post]

I am encountering an issue with posting a form using the AJAX POST method in jQuery. I am using the serialize method to retrieve the form data, but it seems to fail. The problem might be related to the JavaScript files of the Steps Wizard plugin that I am ...

Unable to send multiple cookies using custom headers in Next.js configuration

I am using custom headers to set the cookie in my next.config.js file. The refresh token is successfully set, but for some reason the second token is not being recognized. key: 'Set-Cookie', value: `RefreshTokenKey = " ...

I'm running into an InvalidSelectorError and I could use some assistance in properly defining

As I gaze upon a massive dom tree, my task using NodeJS/Selenium is to locate an element by the title attribute within an anchor tag and then click on the associated href. Despite being a newcomer to regex, I am encountering numerous errors already. Below ...

The functionality of removing a class on the body element is ineffective when using pagepiling.js

After creating a website using pagepiling.js, I implemented a script that adds the 'active' class to the section currently in view. My goal was to add a specific class to the body when my section1 is considered active. Here's the initial app ...

What is the best way to personalize the collector for each individual?

Currently, I have a bot that is capable of collecting messages and replying if it detects a specific word. However, I am facing an issue where the bot keeps creating new collectors every time someone types the word tekong. As a result, the bot ends up resp ...

Establish a predetermined selection for a radio button and its associated checkbox option

I am working on a React material UI group input field that is mapping a dataset. The result consists of one radio button and one checkbox performing the same action. Initially, I attempted to set the state to establish one data item as default. While I fol ...

Tips for reorganizing the JSON data created by Artoo.js?

My file aims to scrape data from a single webpage, but I've hit a roadblock. I initially tried using artoo, request, and cheerio based on my research. Here's the code I have so far: request('http://www.ciclopi.eu/frmLeStazioni.aspx?ID=144&a ...

Error: jwt_decode function has not been declared

I'm currently working on a project and I've hit a roadblock while trying to fetch profile information for the logged-in account using a token. Despite using the JWT-decoding library, I keep encountering the issue where jwt_decode is not defined. ...

Switch body class when the navbar's collapse show class is toggled

There are numerous methods to accomplish this task, but I am seeking the most optimal and efficient approach. My goal is to toggle a custom body class when the .navbar-toggle triggers the .show class on the .navbar-collapse element. I'm hesitant abo ...

Getting the date from a datetime JSON - here's how!

How can I extract the date from a JSON datetime string like 2013-11-09T00:00:00 using either Jquery or JavaScript? ...

Struggling to retrieve the 'this' object using a dynamic string

Within my Nuxt + TS App, I have a method that attempts to call a function: nextPage(paginationName: string): void { this[`${paginationName}Data`].pagination .nextPage() .then((newPage: number) => { this.getData(pagination ...

Is there a way to determine the color of an element when it is in a hover state?

I recently started using the Chosen plugin and noticed that the color for the :hover on the <li> elements is a bright blue. I want to change it to a bold red color instead. https://i.stack.imgur.com/mcdHY.png After inspecting it with the Chrome too ...

React Functional Component fails to update on state changes

I'm in the process of creating a React application where I can input my height and weight to calculate my BMI. The goal is to display the BMI value on a diagram. To keep things organized, I decided to break down the functionality into smaller componen ...