score:98

Accepted answer

if you need to handle dom events not already provided by react you have to add dom listeners after the component is mounted:

update: between react 13, 14, and 15 changes were made to the api that affect my answer. below is the latest way using react 15 and es7. see answer history for older versions.

class movieitem extends react.component {

  componentdidmount() {
    // when the component is mounted, add your dom listener to the "nv" elem.
    // (the "nv" elem is assigned in the render function.)
    this.nv.addeventlistener("nv-enter", this.handlenventer);
  }

  componentwillunmount() {
    // make sure to remove the dom listener when the component is unmounted.
    this.nv.removeeventlistener("nv-enter", this.handlenventer);
  }

  // use a class arrow function (es7) for the handler. in es6 you could bind()
  // a handler in the constructor.
  handlenventer = (event) => {
    console.log("nv enter:", event);
  }

  render() {
    // here we render a single <div> and toggle the "aria-nv-el-current" attribute
    // using the attribute spread operator. this way only a single <div>
    // is ever mounted and we don't have to worry about adding/removing
    // a dom listener every time the current index changes. the attrs 
    // are "spread" onto the <div> in the render function: {...attrs}
    const attrs = this.props.index === 0 ? {"aria-nv-el-current": true} : {};

    // finally, render the div using a "ref" callback which assigns the mounted 
    // elem to a class property "nv" used to add the dom listener to.
    return (
      <div ref={elem => this.nv = elem} aria-nv-el {...attrs} classname="menu_item nv-default">
        ...
      </div>
    );
  }

}

example on codepen.io

score:2

i recommend using react.createref() and ref=this.elementref to get the dom element reference instead of reactdom.finddomnode(this). this way you can get the reference to the dom element as an instance variable.

import react, { component } from 'react';
import reactdom from 'react-dom';

class menuitem extends component {

  constructor(props) {
    super(props);

    this.elementref = react.createref();
  }

  handlenvfocus = event => {
      console.log('focused: ' + this.props.menuitem.caption.touppercase());
  }
    
  componentdidmount() {
    this.elementref.addeventlistener('nv-focus', this.handlenvfocus);
  }

  componentwillunmount() {
    this.elementref.removeeventlistener('nv-focus', this.handlenvfocus);
  }

  render() {
    return (
      <element ref={this.elementref} />
    )
  }

}

export default menuitem;

score:2

here is a dannyjolie more detailed answer without need of component reference but using document.body reference.

first somewhere in your app, there is a component method that will create a new custom event and send it. for example, your customer switch lang. in this case, you can attach to the document body a new event :

setlang(newlang) {
    // lang business logic here
    // then throw a new custom event attached to the body :
    document.body.dispatchevent(new customevent("my-set-lang", {detail: { newlang }}));
  }

once that done, you have another component that will need to listen to the lang switch event. for example, your customer is on a given product, and you will refresh the product having new lang as argument.

first add/remove event listener for your target component :

  componentdidmount() {
    document.body.addeventlistener('my-set-lang', this.handlelangchange.bind(this));
  }

  componentwillunmount() {
    document.body.removeeventlistener('my-set-lang', this.handlelangchange.bind(this));
  }

then define your component my-set-langw handler

  handlelangchange(event) {
    console.log("lang has changed to", event.detail.newlang);
    // your business logic here .. this.setstate({...});
  }

score:4

first off, custom events don't play well with react components natively. so you cant just say <div onmycustomevent={something}> in the render function, and have to think around the problem.

secondly, after taking a peek at the documentation for the library you're using, the event is actually fired on document.body, so even if it did work, your event handler would never trigger.

instead, inside componentdidmount somewhere in your application, you can listen to nv-enter by adding

document.body.addeventlistener('nv-enter', function (event) {
    // logic
});

then, inside the callback function, hit a function that changes the state of the component, or whatever you want to do.

score:21

you could use componentdidmount and componentwillunmount methods:

import react, { component } from 'react';
import reactdom from 'react-dom';

class movieitem extends component
{
    _handlenvevent = event => {
        ...
    };

    componentdidmount() {
        reactdom.finddomnode(this).addeventlistener('nv-event', this._handlenvevent);
    }

    componentwillunmount() {
        reactdom.finddomnode(this).removeeventlistener('nv-event', this._handlenvevent);
    }

    [...]

}

export default movieitem;

Related Query

More Query from same tag