question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch is only draggable, not toggled by click

See original GitHub issue

I’m on React 16.12.0 and react-switch 5.0.1

When I render react-switch it is only draggable, but does not change/toggle when I click it. I found this issue however that seemed related to an iFrame which I’m not using.

I put a console.log(checked) in the handleChange function and it always outputs false regardless of what the state of the switch is.

Below is the component I’m loading the switch in. My only guess is it might have something to do with the async methods or with me setting the state in the View() function, but it’s not clear to me what I’m doing wrong.

import React, { Component } from "react"
import axios from 'axios';
import Switch from "react-switch";

import HidingContainer from "../HidingContainer"
import NumericValue from "../NumericValue/index"


class HotTub extends Component {
  constructor() {
    super();
    this.state = { checked: false };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(checked) {
    this.setState({ checked });
  }

  async componentDidMount() {
    await this.View();
  }

  CustomSwitch = () => {
    return (
      <Switch
        onChange={this.handleChange}
        checked={this.state.checked}
        onColor="#86d3ff"
        onHandleColor="#2693e6"
        handleDiameter={30}
        uncheckedIcon={false}
        checkedIcon={false}
        boxShadow="0px 1px 5px rgba(0, 0, 0, 0.6)"
        activeBoxShadow="0px 0px 1px 10px rgba(0, 0, 0, 0.2)"
        height={20}
        width={48}
        className="react-switch"
        id="material-switch"
      />
    )
  }

  async getHotTubStatus() {
    const response = await axios({
      url: "http://localhost:3000/hot-tub-status",
      method: 'get'
    });

    return response.data
  }

  async View() {
    const hotTubStatus = await this.getHotTubStatus()

    this.setState({
      checked: hotTubStatus.on, // sets initial state
      View: (
        <NumericValue value={hotTubStatus.temperature} unit="°F" />
        <this.CustomSwitch />
      )
    })
  }

  render() {
    return (
      <HidingContainer metricsRef={React.createRef()}>
        {this.state.View}
      </HidingContainer>
    )
  }
}

export default HotTub

Any insight/help appreciated

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:16
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
MrWilliancommented, Jul 18, 2020

UP! I have the same problem.

3reactions
adarshaacharyacommented, Jul 19, 2020

can you tell me how to make the circle part offcolor when i’m not clicking or focusing it !

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to toggle JQueryUI Draggable on/off in an if statement
Calling .draggable('enable') or .draggable('disable') is a way to enable or disable an already-configured draggable element. It's a way ...
Read more >
staticClick event doesn't trigger if draggable: false #51 - GitHub
I implemented an option that goes to the next cell when the Flickity div is clicked, but there is no staticClick event if...
Read more >
Use your Chromebook touchpad - Google Support
You can use your Chromebook's touchpad to right-click, switch between tabs, and more. Here's how it works:
Read more >
react-switch - npm
Prop Type Default checked bool Required onChange (, , ) func Required disabled bool false
Read more >
How to differentiate mouse “click” and “drag” event using ...
For the “mouse down” event 'drag' variable remains false. But as soon as a “mouse move” event is triggered the drag variable is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found