DEV Community

CitronBrick
CitronBrick

Posted on • Edited on

1

Future React Components: ES2022

Hi, here's an accessible checkbox, which is probably the web's 1st React Component, created using ES2022.

Here the state is a private instance variable. Private instance variables, prefixed with #, are supported in ES2022.

class Checkbox extends React.Component {

        #state = {checked: false};

        handleClick=(evt)=>{
            this.setState((state)=>
                ({checked: !state.checked})
            );
        }

        handleKeyDown=(evt)=>{
            if([' ','Enter'].includes(evt.key)) {
                this.handleClick();
            }
        }

        render() {
            return <div className="checkbox" 
                id={this.props.id} 
                role="checkbox" 
                aria-checked={this.state.checked} 
                aria-disabled={this.props.disabled} 
                onClick={this.handleClick} 
                onKeyDown={this.handleKeyDown} 
                tabIndex={!this.props.disabled? "0":null}>
                    {this.state.checked ? "":""}
            </div>
        }

    }
Enter fullscreen mode Exit fullscreen mode

Unfortunately the latest Babel version (7.0.0-beta.3), does not support it yet (14 July 2022). We'll have to wait, before using it with JSX.

If you don't like creating components using the class keyword, here's another article that shows how to create class compomnents without it. (Internet Explorer 11 compatible)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more