Reactivity Issue with Array in JavaScript and Vue

In my .vue file, I have the following code snippet:

<template>
  <button @click="entries.push(3)">
    Press me!
  </button>
  <ul>
    <li v-for="entry in entries" :key="entry">
      {{ entry }}
    </li>
  </ul>
</template>

<script setup>
const entries = [1, 2]
</script>

When I click the button, I expect a third li element to be added but nothing happens. What could be causing this issue?

Answer №1

I created a playground with the solution implemented: https://codesandbox.io/s/beautiful-pond-8jg5zw?file=/src/App.vuee

<template>
  <button @click="entries.push(3)">Click me!</button>
  <ul>
    <li v-for="entry in entries" :key="entry">
      {{ entry }}
    </li>
  </ul>
</template>

<script setup>
import { ref } from "vue";

const entries = ref([1, 2]);
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Remember to use the

import { ref } from "vue";

Suggestion: Understand the distinction between Ref and Reactive

https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref

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

Modifying the placeholder text in a Blueprint MultiSelect input field

Is there a way to change the default input text "Search..." in the MultiSelect component from Blueprint Labs? I checked the documentation and the example page, but couldn't find a way to customize the placeholder text. I thought it might be similar to ...

Update the input field value dynamically using jQuery upon creation

Is there a more efficient way to change the value of an input field when it's dynamically created using jQuery? For example, when clicking on an "Add" button, a new element appears and triggers a GET call resulting in the following HTML code: <tr ...

Issue with Multiple File Upload Functionality in Dropzone.js + Laravel (Only allowing one file to be uploaded)

Looking for assistance in uploading multiple files using AJAX with Dropzone.js plugin. This is what I have implemented so far - HTML (view)- <div class="dropzone" id="add-slide-image"> </div> JS- Dropzone.autoDiscover = false; var myDropzo ...

Can you explain the distinction between angular-highcharts and highcharts-angular?

Our team has been utilizing both angular-highcharts and highcharts-angular in various projects. It appears that one functions as a directive while the other serves as a wrapper. I'm seeking clarification on the distinctions between the two and recomme ...

Here is a unique variation: "Learn how to duplicate a designated div element from "a.html" and transfer it to "b.html" with

Is there a way to streamline the process of updating a navbar on multiple pages by storing it in a separate HTML file and importing it into all pages using jQuery? This would allow for easy updates to be made once in the distinct file, automatically reflec ...

Angular directives are not triggering window level events as anticipated

This code snippet might be a bit confusing. Take a look at the code below: //html <div id="test1" test></div> <div id="test2" test></div> //module var myApp = angular.module(&ap ...

Place form at the center of the Bootstrap Modal

My question is, how can I center the entire form, including the input fields and labels, within the modal? Here is also a helpful Fiddle: http://example.com, although the snippet provided should work as well. <script src="https://example.com/bootstr ...

What is the method to update the text area with the help of useState in React

I am dealing with a textarea array that needs to be updated whenever new text is entered or when the array itself is externally updated. However, I have encountered an issue where Textarea does not seem to update its values properly using setState() as r ...

Can TypeScript modules be designed to function in this way?

Seeking to create a versatile function / module / class that can be called in various ways: const myvar = MyModule('a parameter').methodA().methodB().methodC(); //and also this option should work const myvar = MyModule('a parameter') ...

jQuery Color Plugin - The issue is that $.Color is not registering as a function

I have been attempting to get the demo from jquery-color up and running, but I keep encountering the error message $.Color is not a function. Can anyone point out what mistake I might be making? Here is the relevant snippet of code: $("#sat").click(funct ...

Converting a Class Component to a Functional Component in React: A Step-by-Step

I need to refactor this class-based component into a functional component class Main extends Components{ constructor(){ super() this.state = { posts:[ { id:"0", description:"abc", imageLink: ...

Utilizing ASCX to trigger JavaScript functions within an ASPX page

I created a user control named MyListSelect.ascx that contains a list of radio buttons... <%@ Control Language="C#" %> <select> <option disabled selected>Select Options</option> <option> option 1 </opti ...

Unable to execute PHP alongside a JavaScript event listener

Using PHP, I am creating a canvas for writing and the text output will appear in a textarea (handled by other functions). There are additional input tags like a title to gather user input. The values from these input tags (title and textarea) will be submi ...

"Mastering the art of running a successful Dojo

As I delve into learning dojo, I encountered some peculiar syntax in a project on GitHub. dojo.declare('app.services.Favorites', [ dojo.store.Memory ], { constructor : function() { this.inherited(arguments); dojo.subscribe(& ...

The TypeScript compiler does not allow a 'number' type to be assigned to 0, 10, or 20, even when the number itself is 0

When testing out my code snippet on the playground for Typescript, an error appears on line 24. I discovered that the issue can be resolved by explicitly casting commands back to <IPlan[]>, but I wonder why this extra step is necessary. Property &a ...

Transferring information from the main function to getServerSideProps

I've been facing challenges while trying to pass data from a function component to the getServerSideProps method in Next.js. As a beginner in learning Next.js, I am struggling to understand this concept. My approach involves using context from _app, b ...

An issue has occurred in Angular pipe: TypeError - array.map is not functioning as expected

I encountered the following error: ERROR TypeError: array.map is not a function, while using this angular pipe. Here is the code snippet: export class CharacterWithCommasPipe implements PipeTransform { transform(array) { return array.map(item => ...

What are the steps for creating an electron app using Next.js version 13.4?

Embarking on a fresh project, my aim is to create a desktop application using the powerful duo of Next.js 13.4 and Electron. Surprisingly, I have had difficulty in locating any boilerplates specifically tailored for this combination. Despite experimenting ...

Could slots in Vue potentially have a negative impact on the overall design?

This afternoon, I dived into learning about slots in Vue. While I believe I have a good grasp on how to code them, I can't help but wonder if they are counterintuitive when it comes to design. Vue emphasizes building self-contained components with cl ...

How do I connect the HTML file with a JS library that I have installed in WebStorm?

Currently using WebStorm and recently installed jQuery through its built-in feature. Noticed that it is now visible in the External Libraries section of my project. However, I am facing issues linking my Html files to it (<script src="???"></scri ...