VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-force-a-vue-component-to-re-render/

⇱ How to Force a Vue Component to Re-Render? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Force a Vue Component to Re-Render?

Last Updated : 30 Sep, 2024

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.

Step 6: Modify the Main App Component

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

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
Comment
Article Tags: