Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. class MovieItem extends React.Component {
  2.  
  3. componentDidMount() {
  4. // When the component is mounted, add your DOM listener to the "nv" elem.
  5. // (The "nv" elem is assigned in the render function.)
  6. this.nv.addEventListener("nv-enter", this.handleNvEnter);
  7. }
  8.  
  9. componentWillUnmount() {
  10. // Make sure to remove the DOM listener when the component is unmounted.
  11. this.nv.removeEventListener("nv-enter", this.handleNvEnter);
  12. }
  13.  
  14. // Use a class arrow function (ES7) for the handler. In ES6 you could bind()
  15. // a handler in the constructor.
  16. handleNvEnter = (event) => {
  17. console.log("Nv Enter:", event);
  18. }
  19.  
  20. render() {
  21. // Here we render a single <div> and toggle the "aria-nv-el-current" attribute
  22. // using the attribute spread operator. This way only a single <div>
  23. // is ever mounted and we don't have to worry about adding/removing
  24. // a DOM listener every time the current index changes. The attrs
  25. // are "spread" onto the <div> in the render function: {...attrs}
  26. const attrs = this.props.index === 0 ? {"aria-nv-el-current": true} : {};
  27.  
  28. // Finally, render the div using a "ref" callback which assigns the mounted
  29. // elem to a class property "nv" used to add the DOM listener to.
  30. return (
  31. <div ref={elem => this.nv = elem} aria-nv-el {...attrs} className="menu_item nv-default">
  32. ...
  33. </div>
  34. );
  35. }
Add Comment
Please, Sign In to add comment