Open In App

How to Force a Vue Component to Re-Render?

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In VueJS, you may sometimes need to force a component to re-render, especially when the component doesn't update automatically due to Vue's reactivity system not detecting changes. One way to achieve this is by using a unique key attribute. Changing the key will force Vue to destroy and recreate the component, effectively re-rendering it.

Step-by-Step Setup and Installation

Step 1: Install Vue CLI

If you haven't installed Vue CLI globally, you can do so with npm:

npm install -g @vue/cli

Step 2: Create a New Vue Project

Create a new Vue project named force-re-render-example:

npm init vite@latest vue-rerender-example -- --template vue

Step 3: Navigate to the Project Directory

Move into the project directory:

cd force-re-render-example
npm install

Step 4: Create a New Component

Create a new file named RandomNumber.vue inside the src/components directory:

cd src/components/
touch RandomNumber.vue

Project Structure:

file
Folder Structure

Updated Dependencies:

"dependencies": {
"core-js": "^3.8.3",
"vue": "^3.2.13"
}

Approach

  • Data Initialization: The component initializes a random number (randomNumber) and a componentKey to uniquely identify the component instance.
  • Render Logic: The random number is displayed using Vue’s interpolation syntax ({{ randomNumber }}) within the template.
  • Force Re-render: A method forceReRender() is triggered when the button is clicked, incrementing componentKey. This can be used to force the component to re-render.
  • Re-render Mechanism: Although componentKey is updated, it must be bound (e.g., using :key="componentKey") for Vue to recognize the change and force a re-render.

Step 5: Add Code to RandomNumber.vue

This Vue.js component displays a random number and includes a button to trigger a re-render. The random number is initialized in the data object and shown in the template. A method forceReRender() increments a componentKey to force a re-render, although the componentKey needs to be bound (e.g., :key="componentKey") for the re-render to take effect and update the random number. The component includes scoped styles for custom CSS.

Paste the following code into RandomNumber.vue. This component displays a random number and includes a button to force a re-render.

JavaScript
<!-- src/componnets/RandomNumber.vue -->
<template>
  <div>
    <p>Random Number: {{ randomNumber }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
    // Generate a random number
      randomNumber: Math.random(), 
    };
  },
};
</script>

<style scoped>
/* Add any component-specific styles here */
</style>

Step 6: Modify the Main App Component

Open App.vue in the src directory and modify it to include the RandomNumber component.

JavaScript
<!-- src/App.vue -->

<template>
  <div id="app">
    <h1>Force Vue Component Re-render</h1>
    <!-- Use the component key to force re-render -->
    <RandomNumber :key="componentKey" />
    <button @click="forceReRender">Re-render Component</button>
  </div>
</template>

<script>
import RandomNumber from "./components/RandomNumber.vue";

export default {
  components: {
    RandomNumber,
  },
  data() {
    return {
      componentKey: 0, // Unique key for the component
    };
  },
  methods: {
    forceReRender() {
      // Increment the key to force re-render
      this.componentKey += 1;
    },
  },
};
</script>

<style>
/* Add any global styles here */
</style>

Step 7: Run the Application

Start the development server to see the component in action:

npm run serve
  • Open the provided URL (usually http://localhost:8081) in a web browser.
  • You should see the "Force Vue Component Re-render" heading, a random number, and a button to re-render the component.

Output:

force-re-render-example-GoogleChrome2024-09-2711-01-24-ezgifcom-video-to-gif-converter
Output

Next Article

Similar Reads