Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Building a toggle switch widget using react js

I am trying to build a toggle widget. This is the code I am trying to execute. Here, I am facing one issue if the toggle is in the true state it is toggling to the false state properly. But when it is the false state it is not toggling. I am new to this react js. Could anyone please help me to solve this issue? Thanks in Advance.

import { Component, createElement } from "react";
import { CheckboxToggle } from "react-rainbow-components";

export default class SwitchExample extends Component {
    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(event) {
        const { booleanAttribute } = this.props;
        booleanAttribute.setValue(!event.target.value);
    }

    render() {
        const { booleanAttribute } = this.props;
        return <CheckboxToggle value={booleanAttribute.value} onClick={this.handleChange} />;
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You no need to get value from e.target.value instead you can use callback to update state as:

codesandbox demo

  handleChange(event) {
    const { booleanAttribute } = this.props;
    booleanAttribute.setValue((value) => !value);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading