2

I am trying to use Semantic UI react for layout. These are the following sample code I am having trouble with onChange. I can check to click the toggle but resets everytime I refresh.

import {
  Checkbox
} from 'semantic-ui-react'

onChangeInput(event) {
    let name = event.target.name
    let value = event.target.value

    let talent = this.state.newTalentProfile
    talent[name] = value

    this.setState({
      newTalentProfile: talent
    })

  }
  
  <Select
name = "willing_to_relocate"
ref = "willing_to_relocate"
defaultValue = {this.props.talent.willing_to_relocate}
onChange = { this.onChangeInput.bind(this)} >
  <Option value = ""label = "" / >
  <Option value = "YES"label = "YES" / >
  <Option value = "NO"label = "NO" / >
  </Select>

the below code doesn't work, but the above one works when i make changes it saves it to database

<Checkbox toggle
name = "willing"
ref = "willing"
label = "Willin To Relocate"
onChange = {this.onChangeInput.bind(this)
}

/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>

3 Answers 3

2
  onChangeCheckbox = (evt, data) => {
    let checked = data.checked
    console.log(checked)
  }


 <Checkbox toggle label='Running discounted price?' 
  onClick={(evt, data)=>this.onChangeCheckbox(evt, data)}
   />

This should do the trick.

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

Comments

1

Your code suggests you expect event.target to be a checkbox element, but it is not. If you examine the variable event.target within your browser, you'll notice that it points to a label element, not the checkbox itself. Therefore, I'm guessing that your event.target.value and event.target.label are evaluating to null, causing the rest of your code to not function the way you expect.

There's many ways to get around this. One way is to set a state variable for your component to represent the state of the checkbox:

class TheComponentThatContainsMyCheckbox extends Component {
    state = {
     textValue: null,
     willingToRelocate: true
    }
...

Then, you can create a handler that toggles this state when checked:

toggleCheckBox = () => {
  const willingToRelocate = !(this.state.willingToRelocate);
  this.setState({willingToRelocate}); 
  // Note: if you have other logic here that depends on your newly updated state, you should add in a callback function to setState, since setState is asynchronous
}

Notice that I'm using arrow syntax here, which will automatically bind this for me. Then, hook it up to the onChange event of the Semantic UI Checkbox:

<Checkbox label='Willing to Relocate.' onChange={this.toggleCheckBox}/>

You can now use your this.willingToRelocate state to decide other application logic, passing it up or down using whatever state management pattern you like (state container/store like Redux, React Context, etc.).

Comments

0

I assume your this.onChangeInput handles database updates.

semantic ui react offers a prop checked for Checkbox.

You should have a state {checked: false} in your code, then this.setState whatever in componentDidMount after retrieving the database record.

Pass this.state.checked into checked prop of Checkbox.

Comments

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.