Nuxt.JS is not rendering blocks in the way that I require

I am currently developing a nuxt.js SSR web application and encountered an unusual issue.

<div class="container" @mouseenter="hovered = true" @mouseleave="hovered = false">
  <transition name="fade">
    <div class="class1" v-show="!hovered && isDesktop">blank_1</div>
  </transition>
  <transition name='scale'>
    <div class="class2" v-show="hovered || !isDesktop">blank_2</div>
   </transition>
</div>

<script>
export default {
  data() {
    return {
      hovered: false
    }
  },
  computed: {
    isDesktop() {
      if(process.client) {             
        window.screen.width > 1024 ? return true : return false
      }
    }
  }
}
</script>

<style>
.class1 {
  height: 100px;
  width: 100px;
  background-color: red;
}
.class2 {
  height: 100px;
  width: 100px;
  background-color: blue;
}
</style>

Allow me to break down this situation:

  1. Explanation of expected behavior:
    • The div with class "class1" should be visible by default on Desktop, but disappear when hovering over the container. On mobile devices, it should remain hidden at all times.
    • The div with class "class2" should be hidden on Desktop, but become visible upon hovering over the container. On mobile devices, it should always be displayed.
  2. Current functionality:
    1. On Desktop:
      • The div with class "class1" remains hidden until I hover over the container once, then behaves as intended.
      • The div with class "class2" functions correctly.
    2. On Mobile:
      • The div with class "class1" works as expected.
      • The div with class "class2" works as expected.
  3. Solution:
    • After several hours of troubleshooting, I realized that I could update the v-show directive on the first div to v-show="!hovered" and apply a media query for mobile screens display:none;. This resolved the issue specific to Desktop usage.

However, why does it behave differently when using

v-show="!hovered && isDesktop"
? I assumed that during the initial Nuxt.JS load on the server, the isDesktop value is returned as undefined, causing
v-show="!hovered && isDesktop"
to evaluate to
v-show="!false && undefined"
. Despite this, why does the second div's v-show directive: v-show="hovered || !isDesktop" function correctly, even though it should result in v-show="false || !undefined", displaying the div on Mobile and hiding it on Desktop.

P.S. This marks my inaugural post on StackOverflow, apologies if the code formatting is subpar—I'm still learning how it operates. Thank you in advance for any insights provided.

Answer №1

This unusual behavior can most likely be attributed to the following line:

window.screen.width > 1024 ? return true : return false

It should actually be written as:

return window.screen.width > 1024 ? true : false

The syntax is incorrect and when I attempted to compile it in Nuxt, it did not work. It's intriguing how your Nuxt server is able to handle this error.

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

Identify the specific dependency array variable that triggered the useEffect hook to be executed

Is there a simple way to identify which variable in a useEffect's dependency array is prompting a function re-firing? Merely logging out each variable could be deceiving; for instance, if a is a function and b is an object, they might seem identical ...

Personalized FullCalendar header title

Is there a way to display a unique header title for each calendar in my collection of 16? I've been trying various modifications to the code snippet below with no success: firstDay: <?php echo $iFirstDay; ?>, header: { left: 'prev,next ...

Exploring the Wonderful World of Styled Components

I have a query regarding styled components and how they interact when one is referenced within another. While I've looked at the official documentation with the Link example, I'm still unclear on the exact behavior when one styled component refe ...

The login button has dual functionality, redirecting users to different pages based on their login status. First-time logins will be directed to an agreement page, while returning users will be

During my testing of login functionality, I follow these steps: Enter username Enter password Click on the login button Verify if the agreement page is displayed This is how my Page Object Model (POM) for the login page looks like: class loginPage { ...

Identifying sluggish GPU performance on a mobile device using three.js: A guide for developers

My game runs extremely slow on older mobile devices like the Samsung Galaxy S4 and iPhone 5 when shadows are enabled. However, when shadows are turned off, performance improves significantly. Is there a way to detect a slow GPU in order to disable sh ...

Switching to '@mui/material' results in the components failing to render

I have a JavaScript file (react) that is structured like this: import { Grid, TextField, makeStyles } from '@material-ui/core' import React from 'react' import {useState} from 'react' //remove this function and refresh. see ...

Scroll the div back to the top when the form is submitted

I have set up a form in a pop-up using Bootstrap modal. The form is quite long, so after submission, the message appears at the top of the form. However, I want it to scroll to the top when the user submits the form so they can easily see the message. Is ...

Vue.js HTML not refreshing after axios request changes object property

Issue with Vue.js not updating HTML after axios GET request. Here's the structure: <span @click="tryResponse"> Data_object: {{ data_object }} <br> Data_object.serial: {{ data_object.serial }} <br> </span> Data ...

``What is the best method for retrieving query parameters in the _app function?

My technology stack: React.js + Next.js I am trying to extract query parameters in app.jsx. When I included the following code: console.log(1); console.log(router); //(= useRouter import from next/router) console.log(2); console.log(Router); //(= Router ...

Guide to displaying options in the Vue material select box even when the default value is blank

I have implemented the material design framework vuematerial with vue.js. In traditional HTML, a selection box looks like this: <select> <option value="">You should initially see this</option> <option value="1">One</o ...

inside the connect module, the res._renderHeaders function is located

Currently, I am delving into the patch.js file within the connect module. In it, I found some intriguing code snippets: var http = require('http') , res = http.ServerResponse.prototype , setHeader = res.setHeader , _renderHeaders = res._re ...

What is the best way to capture a form submission when there are no errors?

My form includes a validation check for valid fields upon submission. Here is an example of how it is structured: <form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" /> <div class="con ...

axios encountered a JSON parsing issue due to an unexpected character in the server's response

I'm encountering an issue with JSON parsing as I attempt to retrieve data from a server endpoint. This is the first instance where Axios is unable to automatically decode the JSON response. Upon inspecting my code, I noticed that Axios is detecting ...

Limiting the number of characters in a textarea using React with TypeScript

Having encountered two issues, I've developed a textarea component that not only allows users to input text but also keeps track of the number of characters they have typed. First Issue: I'm attempting to check if the length of the current input ...

Efficiently sift through a vast assortment using a filtering method

Currently, I am developing an application similar to Uber which involves managing a collection of drivers with their current positions (latitude and longitude). One specific requirement is to find drivers who are within a 200-meter distance from the user& ...

Under what circumstances would you need to manually specify the displayName of a React component?

After coming across an article (linked here: https://medium.com/@stevemao/do-not-use-anonymous-functions-to-construct-react-functional-components-c5408ec8f4c7) discussing the drawbacks of creating React components with arrow functions, particularly regardi ...

Utilizing Vue Cli's Webpack Proxy feature

While working on a project using the vue-cli and the Webpack template, I am facing some difficulties with setting up a custom host. Currently, Webpack is listening to localhost:8080, but I need it to work with a custom domain like . Has anyone found a solu ...

Using Vue to toggle an active HTML class on click: A tutorial

Can someone assist me with this code snippet that is intended to toggle a class using a Vue on-click event? I'm encountering issues as it's not working and I'm seeing several errors in the console. Any help would be appreciated. <div id=& ...

Pausing repetitive HTTP requests in an Angular 6 project within a do while loop

While waiting for the completion of one API call, I am recursively consuming an external API. The http calls are being made using import {HttpClient} from '@angular/common/http' As a newcomer to the framework, there may be something wrong in the ...

Using HTML5 and an ASP.NET web method to allow for file uploads

My attempt to upload a file to the server using an HTML5 input control with type="file" is not working as expected. Below is the client-side JavaScript code I am using: var fd = new FormData(); fd.append("fileToUpload", document.getElementById(&ap ...