Trouble encountered when attempting to call a function within another function in StencilJS

I am currently following a tutorial on building a drag and drop file uploader using StencilJS for some practice and fun. However, I have encountered an error in the code.

Below is a snippet of the code, but I can provide more if necessary.

@Component({
  tag: 'dynamic-uploader',
  styleUrl: 'dynamic-uploader.css',
  shadow: true,
})
export class DynamicUploader {
  @Element() public dynamicUploader: HTMLElement;
  @State() dropzoneActive: boolean = false;
  @State() stagedFiles: Array<StagedFile> = [];

  componentDidLoad() {
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
      this.dynamicUploader.addEventListener(eventName, this.handleDefaultPrevention, false);
      document.body.addEventListener(eventName, this.handleDefaultPrevention, false);
    });

    ['dragenter', 'dragover'].forEach(eventName => {
      this.dynamicUploader.addEventListener(eventName, () => (this.dropzoneActive = true), false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
      this.dynamicUploader.addEventListener(eventName, () => (this.dropzoneActive = false), false);
    });

    this.dynamicUploader.addEventListener('drop', this.handleDroppedFile, false);
  }

  handleDefaultPrevention(event: Event) {
    event.preventDefault();
    event.stopPropagation();
  }

  handleFiles(file)  {
    //   files = [...files];
    //   files.forEach(this.handleFilePreview);
  }

  // handleFiles = files => {
  //   files = [...files];
  //   files.forEach(this.handleFilePreview);
  // };

  handleDroppedFile(event: InputEvent) {
    let dataTransfer = event.dataTransfer;
    let files = dataTransfer.files;
    console.log(this.dynamicUploader);

    Array.from(files).forEach(i => this.handleFilePreview(i));
  }

  handleFilePreview(file: File) {
    this.stagedFiles = [...this.stagedFiles, <staged-file file={file}></staged-file>];
  }

  render() {
    return (
      <Host>
        {`The dropzone is ${this.dropzoneActive ? 'active' : 'inactive'`}
        <form id="dynamic-uploader" class={{ 'is-active': this.dropzoneActive }}>
          <p>Upload Evidence</p>
          <input type="file" id="file-input" multiple accept="image/*" />
          <label class="button" htmlFor="file-input">
            Select some files
          </label>
        </form>
        <div class="stage-files">{this.stagedFiles}</div>
      </Host>
    );
  }
}

It seems that the handleFiles method is not working as expected during debugging. If written as an arrow function, it doesn't seem to exist, which led me to believe there were scope issues. However, rewriting it in the conventional way does not resolve the issue either.

Answer №1

The handleFile function has not been invoked in the code, causing the TypeScript compiler to exclude it during its optimization process.

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

Collaborating on a project that has been developed using various editions of Typescript

Currently, I am part of a team working on a large project using Typescript in Visual Studio. As we have progressed through different versions of the project, we have encountered an issue with versioning the installed TypeScript within Visual Studio. Situa ...

JavaScript Definition File for TypeScript

Within my repertoire is a Js File, comprised of a leaflet plugin, Js: L.BingLayer = L.TileLayer.extend({ options: { subdomains: [0, 1, 2, 3], type: 'Aerial', attribution: 'Bing', culture: '' }, initialize ...

Error in cypress configuration occurs when a plug is mistakenly defined as an import instead of a const within the cypress.config.ts file

Hello, I am new to using Cypress so please bear with me if this seems like a trivial issue. Below you will find my cypress.config.ts file where I am attempting to integrate the cypress esbuild preprocessor into the configuration. import cypress_esbuild_pre ...

Searching for the position of different size values according to their specific value

information = { boxNoTo: 1, boxNoFrom: 1, size: 'M', } items = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }] This is what I have don ...

Guidelines on incorporating emotion/styled into React applications with TypeScript

Including my root component in the ThemeProvider from @emotion/react has granted me access to props.theme. Here is an example: const StyledDiv = styled.div` background-color: ${(props) => props.theme.palette.primary.main}; `; Issue: TypeScript indica ...

Remove the user along with all of their associated documents from Firestore

When a user wants to delete their account, I need to ensure that the 'documents' they created in Firebase are deleted as well. After some research online, I came across the following code snippet: deleteAccount() { const qry: firebase. ...

How can data be transferred between controllers in Angular 2 without using URL parameters or the $state.go() function?

I've encountered an issue where I need to pass a parameter from one controller to another without it being visible in the URL. I attempted to do so with the following code: this.router.navigate(['/collections/'+this.name], {id: this.id}); ...

Creating global variables in NodeJS allows you to access and modify data

Currently, this construct is being utilized to create a global LOG: declare global { let LOG: Logger; } // eslint-disable-next-line @typescript-eslint/no-namespace declare namespace globalThis { let LOG: Logger; } globalThis.LOG = new Logger(); It f ...

Remove an item from the DOM instantly with React

Having trouble synchronously removing a child from the container? Here is a simplified code snippet demonstrating the current solution using the useState hook. type ChildProps = { index: number; id: string; remove: (index: number) => void; }; fun ...

The module named "mongoose" does not have any member called 'PaginateResult' exported

I'm facing an issue while trying to add the necessary types for "mongoose-paginate" in my Angular 4 project setup with "angular-cli". The problem arises when Webpack throws an error. import {PaginateResult} from "mongoose"; ... getAll(page: number) ...

Deactivate specific choices from a dynamically generated dropdown menu in Angular 8

I am working on a dynamic dropdown feature with multiple fields. By pressing the + button, a new row is generated. Users can add any number of rows. My challenge is to prevent users from selecting previously chosen values in the dropdown list. I have ma ...

Navigating through an Angular 2 service

I'm struggling to retrieve an array from a JSON API and then loop through it. I can't seem to grasp how it all fits together. Any guidance would be greatly appreciated. This is what my service looks like: import {Injectable} from '@angular ...

Establishing a connection pathway for communication among components in Angular

I am faced with a situation where I have two components, CompA and CompA5, that are 3 or 4 levels apart. I need to establish a means of communication between these components. For instance, I want component CompA to send an event to CompA5, receive some d ...

Tips for showing a Dialog box in reference to multiple rows in a table

Objective: Retrieve data and showcase it in a dialog box using the button located in the Button column. Essentially, clicking on one of the buttons will display the corresponding data in the dialog. Challenge: Currently, I can only extract hardcoded s ...

The eslint rule 'import/extensions' was not found in the definition

I'm encountering two errors across all TypeScript files in ESLint on VS Code: Not able to find definition for rule 'import/extensions'.eslint(import/extensions) Not able to find definition for rule 'import/no-extraneous-dependencies&apo ...

There seems to be an issue with calling this particular expression. The elements within the type 'string | ((searchTerm: string) => Promise<void>) | []' are not all callable

Here is my unique useResults custom hook: import { useEffect, useState } from 'react'; import yelp from '../api/yelp'; export default () => { const [results, setResults] = useState([]); const [errorMessage, setErrorMessage] = us ...

Oops! The OPENAI_API_KEY environment variable seems to be missing or empty. I'm scratching my head trying to figure out why it's not being recognized

Currently working on a project in next.js through replit and attempting to integrate OpenAI, but struggling with getting it to recognize my API key. The key is correctly added as a secret (similar to .env.local for those unfamiliar with replit), yet I keep ...

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: https://i.sstatic.net/i7eZT.png The issue arises when setting the div&apo ...

Utilizing ag-grid with Vue.js: Implementing TypeScript to access parent grid methods within a renderer

I've integrated ag-grid into my project and added a custom cell renderer: https://www.ag-grid.com/javascript-grid-cell-rendering-components/#example-rendering-using-vuejs-components Although the renderer is working well, I'm facing an issue whe ...

How can I subtract a value from my array in Angular?

I've been troubleshooting this problem for a while now and I'm hoping that someone here can assist me with finding a solution. The issue at hand involves an array object containing various values such as id, title, amountCounter. Specifically, t ...