When working in React, I encountered a problem with the for of loop, as it returned an error stating "x is undefined." Although I could easily switch to using a simple for loop, I find the for of loop to

I'm encountering an issue when using a for of loop in React, as it gives me an error stating that "x is undefined".

import { useEffect } from "react";

export default function HomeContent() {
  useEffect(() => {
    let content = document.getElementsByClassName("content");
    let contentList = [
      "Hi! I am Aaditya",
      "I'm a Freelance Web Designer currently based in New Delhi, India",
    ];
    for (x in contentList) {
      console.log(x);
    }
  }, []);

  return (
    <div className="HomeContent">
      <div className="content"></div>
      <i className="fas fa-sort-down fa-5x next"></i>
    </div>
  );
}

Answer №1

To begin with, the loop you are utilizing is a for in, not a for of. To rectify this, ensure that you declare the x variable like so:

for (let x in contentList)
  console.log(x); 
}

Answer №2

Try this transformation:

for (const item of contentList) {
    console.log(item);
}

Transformed into:

contentList.forEach(item => console.log(item));

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

Chrome now supports clickable circular canvas corners

I have a unique setup with two canvases. I've customized them to be circular by utilizing the border-radius property. The second canvas is positioned perfectly within the boundaries of the first one using absolute positioning. This is where it gets e ...

Issue displaying Windows_NT version 10.0.22621 along with the corresponding logs

NPM issues persisting I keep encountering problems with NPM not working properly, even after attempting npm install multiple times. It keeps generating an npm-debug.log file in another directory. The trouble started when I faced issues with reportwebvital ...

Modifying the hue of Material UI tab label

I attempted to modify the label color of a tab to black, but it seems to be stuck as white. Is this color hard-coded in material-ui? If not, how can I change it? Here's what I've tried: const customStyles = { tab: { padding: '2p ...

Attempting to provide images that are dynamically downloaded in real-time using Express Middleware

I'm facing a challenge with express and middleware. My goal is to serve an image from disk, but if it's not there, download it from an external server and then display it. Subsequent requests for the same image should be served from disk. Downlo ...

Tips on customizing a Drawer with Material-UI v5

When working with Material-UI v4, you could style the Drawer component like this: <Drawer variant="persistent" classes={{paper: myClassNameHere}} > The myClassNameHere is generated by using useStyles, which in turn is created using mak ...

Adjust the positioning of a class element

I have an eye icon that changes position when clicked. Initially, it is set to left: 10%;, but if there is a DOM element with the ID login-section, it should be repositioned to left: 50%;. I attempted to use document.getElementsByClassName('fa-eye-sl ...

Guide to setting up a trigger/alert to activate every 5 minutes using Angular

limitExceed(params: any) { params.forEach((data: any) => { if (data.humidity === 100) { this.createNotification('warning', data.sensor, false); } else if (data.humidity >= 67 && data.humidity <= 99.99) { ...

Tips on concealing a div until the value of a specific field surpasses zero

In order to ensure that users focus on the first section of the form before proceeding, I plan to hide the last section until a specific field has a value greater than zero. Check out my HTML code below: <div class="fieldcontainer"> <label>E- ...

The barcode is not displaying when using javascript:window.print() to print

I am currently developing a Mean Stack App where I have a requirement to display a barcode. To achieve this, I am utilizing an AngularJS directive for generating a 128 barcode, and it is being generated successfully. However, when I attempt to print by cli ...

Testing Vue with Jest - Unable to test the window.scrollTo function

Is there a way to improve test coverage for a simple scroll to element function using getBoundingClientRect and window.scrollTo? Currently, the Jest tests only provide 100% branch coverage, with all other areas at 0. Function that needs testing: export de ...

Is it possible to have nullable foreign keys using objectionjs/knex?

It seems like I'm facing a simple issue, but I can't quite figure out what mistake I'm making here. I have a table that displays all the different states: static get jsonSchema() { return { type: 'object', propert ...

Executing the outer function from within the inner function of a different outer function

Imagine this scenario: function firstFunction() { console.log("This is the first function") } secondFunction() { thirdFunction() { //call firstFunction inside thirdFunction } } What is the way to invoke firstFunction from thirdFunction? ...

Shuffle math calculations involving subtraction by a percentage using node.js or JavaScript

Hello there! If you want to subtract, say 35%, from a number, you can use methods like this: var valueInString = "2383"; var num = parseFloat(valueInString); var val = num - (num * .35); console.log(val); But have you ever wondered how you could randomiz ...

Vue - Utilizing child slots in the render method

Within my component, I am working with a default slot and attempting to enhance the layout by wrapping each item in the slot within a div. However, I am facing an issue where I need to retrieve the classes of one of the slot elements, but the VNode element ...

Can I find a better approach to optimize this code?

How can I refactor this code to move the entire DB query logic into a separate file and only call the function in this current file? passport.use( new GoogleStrategy({ clientID: googleId, clientSecret: clientSecret, callbackURL: ...

Tips on implementing local storage in cypress

I am encountering an issue with a simple test describe('Page Test', () => { it('button has "contact-next-disabled" class', () => { cy.get('.contact-next-disabled') }) }) Upon running the test, cypress ...

Vue - Error Message from Eslint Regarding Absence of Variable in Setup Function

Interestingly, the Vue.js documentation strongly recommends using the <script setup> syntax of the Composition API. The issue with this recommendation is that the documentation lacks depth and it conflicts with other tools (like eslint). Here is an e ...

Utilizing anchorEl in combination with styled() in MUI

Having some trouble anchoring a popover component to a button component. It seems that the issue arises when styling the button using styled(), particularly with emotion. This results in a warning message: MUI: The `anchorEl` prop provided to the componen ...

Having trouble retrieving JSON data using ajax

I am currently working with JSON data that is being generated by my PHP code. Here is an example of how the data looks: {"Inboxunreadmessage":4, "aaData":[{ "Inboxsubject":"Email SMTP Test", "Inboxfrom":"Deepak Saini <*****@*****.co.in>"} ...

How to transform the Material UI functional component Mini variant drawer into a class component in ReactJS

I have been working with the mini variant drawer component from the official material-ui website and I am encountering some issues when trying to convert it into a class component. The errors seem to be related to hooks and are occurring within the node ...