Increment active class using arrow key with React Js

Increment active class using arrow key with React Js

I have a situation where I need to be able to add an 'active' depending if I hit the left arrow key or right arrow key. Essentialy I am just tabbing through a menu. I keep getting undefined returned when I am trying to set my state.

I have a situation where I need to be able to add an 'active' depending if I hit the left arrow key or right arrow key. Essentialy I am just tabbing through a menu. I keep getting undefined returned when I am trying to set my state.

Erro: 'Cannot read property 'cursor' of undefined'

Menu.js

...

class Menu extends Component { constructor(props) { super(props); //set active-state to name of button label this.state = { activeTab: this.props.children[0].props.label, cursor: 0, }; } componentDidMount() { document.addEventListener("keydown", this.handleKeyDown, false); }

componentWillUnmount() { document.removeEventListener("keydown", this.handleKeyDown, false); }

handleKeyDown(e) { const { cursor } = this.state if (e.keyCode === 37 && cursor > 0) { this.setState(prevState => ({ cursor: prevState.cursor - 1 })) } else if (e.keyCode === 39 && cursor < this.children.length - 1) { this.setState(prevState => ({ cursor: prevState.cursor + 1 })) }
} //event handler to set activestate onClickTabItem = tab => { this.setState({ activeTab: tab }); };

render() { const { onClickTabItem, props: { children }, state: { activeTab, cursor } } = this;

return (
  &lt;div className="tabbed"&gt;
    &lt;ul role="tablist"&gt;
      {/*itteriate through tabs and set label*/}
      {children.map((child, i) =&gt; {
        const { label } = child.props;
        return (
          &lt;Tab
            activeTab={activeTab}
            key={label}
            label={label}
            onClick={onClickTabItem}
            tab={i}
            className={cursor === i ? 'active' : null}
          /&gt;
        );
      })}
    &lt;/ul&gt;
    &lt;div className="tab-content"&gt;
      {/*itteriate through content and set label*/}
      {children.map(child =&gt; {
        //if tab has label or active set, otherwise do nohthing
        if (child.props.label !== activeTab) return undefined;
        return child.props.children;
      })}
    &lt;/div&gt;
  &lt;/div&gt;
);

} }

....

Button.js

...

class Tab extends Component { onClick() { const { label, onClick } = this.props; onClick(label); }

render() { const { onClick, props: { activeTab, label, tab } } = this;

let className = "tab-list-item";
let ariaSelected = "";
//set aria-selected to true if  has activeState
if (activeTab === label) {
  ariaSelected += "true";
}

return (
  &lt;li role="presentation"&gt;
    &lt;a
      className={className}
      aria-selected={ariaSelected ? "true" : undefined} //set aria-selected to true otherwise dont append
      onClick={e =&gt; this.onClick(e)}
      role="tab"
      id={"tab" + tab}
      tabIndex="-1"
    &gt;
      {label}
    &lt;/a&gt;
  &lt;/li&gt;
);

} }

...


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project. This video covers the key differences between ReactJS, Angular and Vue with respect to the following: Use case, Performance, Data binding, Scripting language, Testing, Community support, Growth curve

What is JavaScript – All You Need To Know About JavaScript

In this article on what is JavaScript, we will learn the basic concepts of JavaScript.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.