0

For inputs with type text it's easy, with onChange() I update the state calling setState() and showing the state on the input through its value attribute. However, for checkboxes and even more for file, I'm totally lost.

Which would be the:

  1. handler
  2. property of the event object
  3. attribute ... for input>checkbox and input>file

Example of input>text

  1. onChange()
  2. event.target.value
  3. value

1 Answer 1

1

For checkbox you can check using using the following :

handleChange(e) {
  console.log(e.target.checked);
}
<input 
  type="checkbox" 
  name="checkbox"
  onChange={ this.handleChange } 
/>

For files you can go as follows:

handleUploadFile(e) {
  let selectedFile = e.target.files;
  //if single file
  console.log(selectedFile[0]);
  //else loop around the files
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @yash-thakur So, we don´t set the state and assign a value for x-attribute of that element...? Example: <input type="checkbox" checked={this.state.checkedState} onChange={(e) => this.handleChange(e.target.checked)} /> Thanks again!
You can setState int the handleChange function and used that state as per what you mentioned in your above comment.

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.