Implementing a method to pass on an incrementing index condition from one component to another using provide and inject in Vue.js

I attempted to enhance the tabsWrapper component by adding a button that, when clicked, selects the next tab. However, my code isn't functioning as expected despite using familiar patterns. My lack of understanding regarding slots, inject, and provide is hindering my progress. Therefore, I require your assistance.

<template>
  <div class="tab-content" v-show="title == selectedTitle || tabTitles[index] == selectedTitle" >
    <slot />
  </div>
</template>
<script>
import { inject } from 'vue';
export default{
    props:['title'],
    setup(){
        const selectedTitle=inject('selectedTitle')
        const tabTitles = inject('tabTitles')
        const index=inject('index')
        return{
            selectedTitle,
            tabTitles, index
        }
    },
    
}
</script>
 <style scoped>
.tab-content{
    padding: 20px;
}
</style>

Within my tab component, I am injecting the index string and tabTitles array from the TabsWrapper component. Additionally, I have included a condition in v-show to render each new tab based on changes to the index value from the TabsWrapper component.

<template>
  <div class="tabs">
    <div class="tab_header">{{ title }}</div>
    <ul class="tabs_header">
      <li
        v-for="title in tabTitles"
        :key="title"
        @click="selectedTitle = title"  
        :class="{ selected: title == selectedTitle}"
      >
       {{ title }} 
        </li>
        <li @click="next">Next</li>
    </ul>

    <slot />
  </div>
</template>

<!-- Additional components, script, and styling omitted for brevity -->

My usage of the composition API is not extensive, and the pattern I employed to increment the index value was sourced from the Vue.js official documentation. The function is intended to update the index value and display the next tab if the selectedTitle matches any of the tabTitles in the array. Nevertheless, my limited knowledge has impeded my progress.

Each time I click the "Next" button, I encounter the following warning:

[Vue warn]: Maximum recursive updates exceeded. This indicates that a reactive effect is mutating its own dependencies, leading to recursive triggers. Possible sources include the component template, render function, updated hook, or watcher source function.

Answer №1

There is no need to utilize the provide/inject syntax, as a standard parent-child relationship exists between the components.

I recommend revisiting the basics of components and opting for using props instead of provide/inject

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

Retrieving information from the server using Backbone to generate a model and assemble a collection

I need assistance with setting up a model and collection for a URL that returns a list of players in JSON format: http://anexampleproject/api/players My goal is to create a model and collection for this data and then display the name of each player in the ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

React JS: The final input is not updated in the state until after all other

Apologies if I struggle to articulate my issue effectively as English is not my primary language. I have developed a form component (coded in ES6) similar to the following: class Form extends React.Component { constructor(...args) { super(args); ...

Using iron-ajax to bind and save JSON data to a file

I have created the iron-ajax element in my HTML page as shown below: <iron-ajax id="fetch-tweets" body='[{"content": "{{tweets}}"}]' url="/callapi" handle-as="json"> </iron-ajax&g ...

Leveraging IE conditional comments for including CSS or JavaScript files can lead to an increase in the number of HTTP

Our web designer has implemented special pages for Internet Explorer by using IE-specific comments. This means that certain stylesheets are only loaded if the user is using a specific version of IE: <!--[if lt IE 7]> <link type="text/css" rel="st ...

Is there a way to adjust the quantity individually, both increasing and decreasing as needed?

I am currently working on implementing a shopping cart feature using pure JS that allows users to increase and decrease the quantity of items. When the + or - button is clicked, both items in the shopping cart will be affected simultaneously if there are 2 ...

What exactly is the significance of the code snippet "var data = jQuery(msg), script;" in JavaScript?

this snippet is extracted from a Google Chrome extension "search" == request.ajax && $.ajax({ url: request.url, type: "GET", dataType: "html" }).done(function(msg) { if (msg.indexOf("https://login.testabc.com/ ...

Implement admin-on-rest framework within my current project

Currently, I am in the process of building an admin-tool website using Material UI. To handle asynchronous calls, I have integrated Redux Saga into my project for calling services. The Admin on Rest framework offers some beneficial components like the Data ...

guidelines for rendering a Vue component within a Vue file using vue-server-renderer

Starting the rendering component with vuejs is my goal. I currently have a basic node server set up. const Vue = require('vue'); const server = require('express')(); const template = require('fs').readFileSync('index. ...

Using window.location.replace() for redirection after AJAX call succeeds

Attempting to redirect the page after a successful ajax call, the code below is functional: $.ajax( { type: "POST", url: path, data: response1, contentType: "application/json", success: -> window.lo ...

Tips for incorporating a multiple tag search feature within my image collection using JavaScript?

I am facing a challenge with my image gallery search feature. Currently, users can search for images by typing in the title or tag of an image. However, I need to enhance this functionality to allow for multiple tags to be searched at once. For example, if ...

Switch button while moving cursor

I'm struggling with getting this 2048x512 image, which has 4 stages of transition, to work properly. https://i.sstatic.net/1PBvX.png While I know how to switch it to the final stage on hover, I can't seem to figure out how to incorporate a trans ...

React Project Encountering Issues with NPM Run Watch

Hey there! I'm currently trying to use NPM Run Watch in order to view localhost:3000 for my React Project, but I've encountered a strange error that has left me unsure of where to even start troubleshooting. Rubens-iMac:react-redux rubenesquiv ...

Is there a way to implement a targeted hover effect in Vue Js?

I'd like to create a hover button that only affects the nested elements inside it. Right now, when I hover over one button, all the nested elements inside its sibling buttons get styled. Any ideas on how to fix this? <div id="app"> <butto ...

Having trouble transferring data between Vue.JS components

Wondering how to pass data from the parent component (Home route) to the child component (playlist route) using props? Encountering a "TypeError: Cannot read property 'length' of undefined" error in the child component? These two components are c ...

Vue components fail to display properly when transitioning between routes in Vue (version 3) using the Vue Router

Just diving into VueJS version 3 and vue-router. Despite my efforts to troubleshoot by consulting Stack Overflow and Google, the issue remains unresolved. I have defined two routes: /login /admin/index The Problem: While the login route ...

Is randomly pairing 2 datapairs after slicing a JSON array easy or challenging?

There is a JSON file containing an unlimited number of users [{ "fname": "Hubert", "lname": "Maier", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bd ...

Retrieve an array from JSON encoding using AJAX

I have been attempting to retrieve 4 JSON arrays stored in a separate file and include them in my main file using an AJAX request. However, I'm facing difficulties storing these arrays into variables and logging them in the console. Here are the resul ...

`Multiple Autocomplete feature that allows rendering of previously selected items`

I've encountered a slight issue with my autocomplete feature. On the same page, I have two different autocompletes set up. Both of them pull elements via ajax from separate sources and use the _render option to display the items. The problem arises wi ...

Unlocking the Power of Rendering DOM Elements with Protractor

Recently, I began learning protractor and encountered some difficulties extracting text from a DOM object. Here is a snippet of code from an AngularJS application: "<div class="input-group application-item-field"> <input type="te ...