TypeError in Vue 3's render function: Cannot read property of undefined (read 'iconType')
P粉585541766
P粉585541766 2024-02-26 12:10:31
0
1
407

I am migrating my vue application from vue 2 to vue 3. This error occurs when removing the (function: true) option from a functional component. In my vue 2 application I use render functions by passing props, data and listeners as parameters. Now, I have read the official documentation but can't understand how to pass parameters? This is my vue 2 render function.

render(h, {props, data,listeners, children = []}) {
    let moreIcons = {};
    let cType = props.iconType || props.type;
    if(!cType) {
        cType = props.picto ? 'picto' : 'line'; 
    }
    if (cType === 'picto') {
        moreIcons = moreIconsPicto;
    } else if (cType === 'line') {
        moreIcons = moreIconsLine;
    }
    let cColor ='';
    if (props.primary||props.prim) { cColor= 'primary'; }
    else if (props.point||props.pnt) { cColor= 'point'; }
    else if (props.default||props.dft) { cColor= 'default'; }
    else if (!!props.color||!!props.col) { cColor= props.color||props.col; }

    let cSize = '' ;
    if(props.large || props.lg) {cSize= 'large';}
    else if(props.medium || props.md) {cSize= 'medium';}
    else if(props.small || props.sm) {cSize= 'small';}
    else if(props.xsmall || props.xs) {cSize= 'xsmall';}
    else if(props.full) {cSize= 'full';}

    let customSize = props.iconSize || props.size;
    if(parseInt(customSize, 10)) {customSize = customSize + 'px';}

    let iconName = props.icon || children.default().text;
    let options = {
        class: [
            'ur-icon',
            'ur-icon--'+cType,
            cColor && 'ur-icon--'+cColor,
            cSize &&'ur-icon--'+cSize,
            props.spacing && 'ur-icon--spacing'
        ],
        style: {
            fontSize: customSize
        },
        on: listeners
    };
    if(data.class) {
        if(Array.isArray(data.class)) {
            options.class = options.class.concat(data.class);
        }
        if(typeof data.class === 'string' || typeof data.class === 'object') {
            options.class.push(data.class);
        }
    }
    if(data.style) {
        options.style = Object.assign(data.style, options.style);
    }
    let child = [];
    if (moreIcons[iconName] !== undefined) {
        options.class.push('more-icon__'+iconName);
        let svg = moreIcons[iconName];
        if(!props.focusable) {svg = svg.replace('<svg ','<svg focusable="false" ');}
        options.domProps={innerHTML:svg};
    } else {
        // 1. icon class
        options.class.push('fa-'+iconName);
        // 2. type class
        if     ( props.brand )   { options.class.push('fab'); }
        else if( props.fab)      { options.class.push('fab'); }
        else if( props.regular ) { options.class.push('far'); }/* pro only */
        else if( props.far )     { options.class.push('far'); }/* pro only */
        else if( props.light)    { options.class.push('fal'); }/* pro only */
        else if( props.fal)      { options.class.push('fal'); }/* pro only */
        else if( props.old)      { options.class.push('fa'); }/* font-awesome 4.0*/
        else if( props.fa)       { options.class.push('fa'); }/* font-awesome 4.0*/
        else { options.class.push("fas"); }
    }
    return h('i',Object.assign(data, options));
},

How to migrate it to vue 3 rendering function or need to use setting function? If I use setup function then how can I pass data and listener as parameters?

P粉585541766
P粉585541766

reply all(1)
P粉792673958

h Functions should be imported from vue rather than used as parameters, I recommend using a render function with a settings hook like this:

import { defineComponent, h } from "vue";

export default defineComponent({
  props: {
    ...
  },
  setup(props, { slots }) {
    let moreIcons = {};
    let cType = props.iconType || props.type;
     ...
     ....
    return () => h('i',Object.assign(data, options));
  },
});


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template