Updating props in a recursive Vue 3 component proves to be a challenging task

I am facing an issue with two recursive components. The first component acts as a wrapper for the elements, while the second component represents the individual element.

Wrapper Component

<template>
  <div class="filter-tree">
    <filter-tree-item
      v-for="item in items"
      :key="item.id"
      :deep="deep"
      :item="item"
      @on-interact="interact"
    ></filter-tree-item>
  </div>
</template>

<script setup lang="ts"gt;
export interface Props {
  items: Items[];
  deep?: number;
}

const props = withDefaults(defineProps<Props>(), {
  deep: 0
})

const interact = () => {
  console.log(props.deep) // logs always 0
}
</script>

Item Component

<template>
  <p @click="handle">{{ deep }}</p> <!--Shows correct deep-->
  <filter-tree v-if="hasChildren" :items="item.children" :deep="deep + 1"></filter-tree>
</template>

<script setup lang="ts">
export interface Props {
  item: Item;
  deep?: number;
}

const props = withDefaults(defineProps<Props>(), {
  deep: 0,
});

const emits = defineEmits(['on-interact'])

const handle = () => {
  emits('on-interact')
}
</script>

While the deep prop displays correctly in the template, I face an issue when trying to access it within my script. Instead of getting the expected value of 2, which is passed down, I consistently receive the topmost value of 0.

What could possibly be causing this discrepancy? Any insights or solutions would be greatly appreciated.

Answer №1

If you try to approach it that way, take a look at this example because it will definitely work:

const emits = defineEmits<{ (event: 'on-interact', deep: number): void }>();

const handle = () => {
  emits('on-interact', props.deep)
}

For more detailed information, refer to the documentation available at https://vuejs.org/api/sfc-script-setup.html#typescript-only-features

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

Use AJAX request to download file and handle errors in case the file is not found or cannot be downloaded

Trying to implement an ajax request for downloading a file. Here's the code snippet: const downloadFile = (element) => { $.ajax({ url: element.id, type: 'GET', success: (result) => { window.lo ...

Asynchronously download static images with the power of NextJS and TypeScript integration

I have a query regarding my website development using NextJS and TypeScript. The site features a showcase gallery and is completely static. Currently, the initial view shows thumbnails of images. When clicking on a thumbnail, the original image is display ...

Compiling TypeScript into JavaScript with AngularJS 2.0

Exploring the capabilities of AngularJS 2.0 in my own version of Reddit, I've put together a script called app.ts ///<reference path="typings/angular2/angular2.d.ts" /> import { Component, View, bootstrap, } from "angular2/angular2 ...

Node.js: Understanding the issue of a mysterious null value in JSON responses sent to clients

As a beginner in the world of Node.js and JavaScript, I have been struggling to find a solution to my problem even after extensive searching. My current challenge involves sending a JSON object to a Node.js server with an array of 2 elements (longitude an ...

Navigate to a different web page within the body tag without changing the current URL by utilizing a hash symbol

I am working with two HTML files, index.html and about.html. My goal is to display the content of about.html within my index.html without navigating away from it. After observing many websites, I noticed that they often use #about in their URLs to dynamic ...

In Typescript, it mandates that a string value must be one of the values within the object

There is a constant declaration mentioned below: export const Actions = { VIEW: 'view', EDIT: 'edit', }; Imagine there's a function like this: // Ensuring that the action variable below is always a string with value either ...

Exploring how to incorporate Express middleware into Firebase Functions for handling HTTPS requests

Encountering a challenge while using Express middleware with Firebase Functions. In this sample, a function is linked to the app() instance as shown below: app.get('*', (req, res) => { res.send(`Hello ${req.user.name}`); }); exports.autho ...

Clean coding techniques for toggling the visibility of elements in AngularJS

Hey everyone, I'm struggling with a common issue in my Angular projects: app.controller('indexController', function ($scope) { scope.hideWinkelContainer = true; scope.hideWinkelPaneel = true; scope.headerCart = false; scope. ...

The HTML video controls in Safari take precedence over the window.name attribute

When using Safari 8.0.5, the controls attribute for the video element will change the value of window.name to "webkitendfullscreen". This is significant because I rely on using window.name to store client-side data in Safari's private mode, where loca ...

Utilizing the W3Schools CodeColor library across various elements

Looking to provide code examples using a JS code highlighter with an ID? Check out this URL for a demonstration: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_syntax_highlight Want to use this for multiple examples but finding that the ID only ...

Ways to deactivate a button with a designated identification through iteration using jQuery

Can't Figure out How to Deactivate a Button with Specific ID $('.likes-button').click(function(){ var el= this; var button1 = $(el).attr('id'); console.log(button1) $('#button1').attr("disabled",true); }) ...

Identify user input with Python Selenium for keyboard and mouse interactions

As a frequent user of Selenium Browser for my everyday browsing needs, I am looking to implement some code that will be triggered when certain keys are pressed on any page. Initially, I considered loading JavaScript onto each page to track key and mouse in ...

Altering various HTML components with every swipe of SwiperJS

I am new to working with JavaScript and I have integrated a Swiper JS element into my HTML page. Here is the current code I am using: <head> <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css&quo ...

How can we show a React JS component when clicked, without using the App.js file for display?

How can I display a component onclick in React JS without using UseState in a file other than App.js? This code will be in App.js var [clicks, setClicks] = useState(false) return( <> {clicks && &l ...

How come I am receiving the E11000 error from Mongo when I have not designated any field as unique?

Encountering an issue while attempting to save the second document to MongoDB Atlas. The error message reads as follows: Error:MongoError: E11000 duplicate key error collection: test.orders index: orderId_1 dup key: { orderId: null } Despite having no un ...

Is it possible to designate touch as the top finger and always have touch 2 be the bottom finger on the screen?

Is there a way to specify touch1 as the upper finger on the screen and always have touch2 as the lower finger, even if the lower touch is detected first? var touch1 = e.originalEvent.touches["0"]; var touch2 = e.originalEvent.touches["1"]; Can these ...

The type does not contain a property named `sort`

"The error message 'Property sort does not exist on type (and then shoes4men | shoes4women | shoes4kids)' pops up when attempting to use category.sort(). I find it puzzling since I can successfully work with count and add a thousand separato ...

Accessing the value returned by an asynchronous function in Node.js with Electron

As I embark on a new project, my goal is to take user input, process it through a function, and then return the updated value back to the user. Despite being a novice with async functions, I've done extensive research but still can't pinpoint if ...

Is there a way to rigorously validate my HTML, CSS, and JavaScript files against specific standards?

Can modern browsers suppress errors in HTML, CSS, and JS sources? Is there a method to uncover all mistakes, no matter how small they may be? ...

Using React Native components from an external package leads to errors

I created a collection of React Native components by utilizing this template to seamlessly integrate Storybook. Additionally, I incorporated nativewind, which essentially serves as a React Native adaptation of Tailwind CSS. The structure of my components i ...