Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactjs typescript event this undefined

I got the following component written in typescript. (type definitions from definitelytyped.org). I got the onWheel event bound to a function. But when ever it is fired this is undefined, so how am I supposed to access the referenced element this.div and if I would want/need to change the state how should do that?

import React = require('react');

interface fooProps {
    src: string;
}

class foo extends React.Component<fooProps, {}>
{
    private div: HTMLDivElement;

    public onWheel(e: React.WheelEvent): void {
        e.preventDefault();
        e.stopPropagation();

        //Do stuff with the div, but 'this' is undefined!!
        this.div;
    }
    public render(): JSX.Element {
                return (
            <div ref={(ref) => this.div = ref} onWheel= { this.onWheel} >
                <img src={ this.props.src } />
                </div >)
    }
}
like image 502
Rutger Avatar asked Mar 19 '16 19:03

Rutger


People also ask

Why is this undefined In React event handler?

Why is this undefined In React event handler? This is because when you use an arrow function, the event handler is automatically bound to the component instance so you don't need to bind it in the constructor. When you use an arrow function you are binding this lexically.

How do you fix an undefined error in React?

The Quick Fix That's often because the array is a piece of undefined state or an undefined prop. Make sure to initialize the state properly. That means if it will eventually be an array, use useState([]) instead of something like useState() or useState(null) .

Why is this undefined TypeScript?

Undefined is the default value for uninitialized variablesWhenever we declare a variable without initializing it with a value, TypeScript initializes it as undefined .

How do you handle undefined value in React JS?

The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null.


2 Answers

Don't know about Typescript, but I'm guessing it's the same thing as when creating components using the similar ES2015 syntax which will need a constructor, and function binding to make a reference to this.onWheel work.

So in ES2015,

class foo extends React.Component {
  constructor(props) {
    super(props);
    // Magic happens here:
    this.onWheel = this.onWheel.bind(this)
    // Now each instance of foo can use this.onWheel
  }

  onWheel () {
    ....
  }

  render (){
    ....
  }
}
like image 58
dannyjolie Avatar answered Oct 22 '22 05:10

dannyjolie


Another solution if you don't want to bind each function in the constructor is to use lambdas:

class foo extends React.Component {
  constructor(props) {
    super(props);
  }

  // The lambda creates a lexical scope so it's autobound
  onWheel = () => {
    ....
  }

  render () {
    ....
  }
}

You can read more here.

like image 5
César Alberca Avatar answered Oct 22 '22 07:10

César Alberca