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;
);

} }

...


javascript reactjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Mapbox GL Javascript wrapper for ReactJS

react-mapbox-wrapper Mapbox GL Javascript wrapper for ReactJS

JavaScript Expressions in JSX in ReactJs

Welcome, what is JSX Expressions in React Js in Hindi? JSX is an Expression Too After compilation, JSX expressions become regular JavaScript function calls a...

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.