1

What I am trying to do here is set the value of the checkbox to 'checked' (because it is styled as a slider) when I click on the <i className='icon-gear'. I'm having trouble accessing input to change it's state. Am I missing something here or am I going about it the wrong way/what would be the better way to approach it? Thanks ahead of time for the help!

const Slider = () => {
  return (
    <div className="slider-container">
      <label className="switch">
        <input type="checkbox" />
        <span className="slider round" />
      </label>
      <p className="batch-slider-title"> Batch Widget </p>
    </div>
  );
};

class Settings extends Component {
    constructor() {
      super();

     this.state = {
        isWidgetDisplayed: false,
        checked: false,
     };

      this.showWidget = this.showWidget.bind(this);
     }

    showWidget() {
      this.setState({
        isWidgetDisplayed: !this.state.isWidgetDisplayed,
        checked: !this.state.checked,
    });
  }

  mouseOut() {
     this.setState({showing: false});
  }

  mouseOver() {
     this.setState({showing: true});
  }

  render() {
      const batchWidget = this.state.isWidgetDisplayed ? <BatchWidget /> : null;
      const slider = showing => {
      if (showing === true) {
         return <Slider />;
      }
         return null;
      };
     return (
     <div>
        <div className="settings">
           <i className="icon-gear" onMouseOut={() => this.mouseOut()} onMouseOver={() => this.mouseOver()} onClick={this.showWidget} />
         </div>
           {slider(this.state.showing)}
           {batchWidget}
        </div>
      );
     }
   }
1
  • 1
    You're not passing the checked state to your Slider component, and to your input. How were you expecting to modify it without passing anything to it? Commented Aug 8, 2017 at 19:54

1 Answer 1

2

<input type="checkbox" /> should be <input type="checkbox" checked={this.props.checked}/>

And it should receive those props via <Slider checked={this.state.checked}/>

Sign up to request clarification or add additional context in comments.

5 Comments

the input isn't defined in the same component where the sate lives, so while you're generally correct, the exact answer would need to be modified to receive it as props
@azium & jmargolis - that's what I've been trying, however I get thrown a type error saying 'cannot read property state of undefined'
you're almost there! Slider is not a class component so there's no this to get props from!
Now I'm getting property 'props' undefined. I've tried passing it through the Slider function, still can't figure out why I'm getting thrown this error
@azium is right. Either make the Slider a Component, or pass the prop another way. Sry, I don't have more time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.