kentcdodds / advanced-react-patterns-v2

Created with CodeSandbox
https://codesandbox.io/s/github/kentcdodds/advanced-react-patterns-v2
1.51k stars 567 forks source link

First exercise - 01: Switch button never change its state! #28

Closed hawaijar closed 6 years ago

hawaijar commented 6 years ago

Hello,

I was going through the codesandbox link - https://codesandbox.io/s/github/kentcdodds/advanced-react-patterns-v2 to quickly go through the exercises. I'm in the first exercise and for some reason, the toggle button didn't change its state though I applied the setState appropriately. BTW, the final exercise also exhibited the same behavior. Am I missing anything?

Oh, yes the toggle() looks same as it's there in the final exercise of 01.

Thanks, KSM

ofhouse commented 6 years ago

Having the same issue (Using Chrome). 👋 You can resolve it in CodeSandbox by editing the onChangeHandler method in this file src/switch.js:

Replace

  onChangeHandler = () => {
    this.props.onClick()
  }

with

  onChangeHandler = e => {
    this.props.onClick()
    e.preventDefault()
  }

Now it should behave like in the video.

Greetings Felix

hawaijar commented 6 years ago

Yup, that works! 😊

ofhouse commented 6 years ago

Nice 😊 Looked a bit more into it and it seems like there was a regression in this pull request #26 , where the e.preventDefault() call was unintentionally removed.

I made a pull-request #29 which should fix the problem in the future.

kentcdodds commented 6 years ago

Sorry about that! Thanks for the help!