mercredi 14 novembre 2018

React syntax for setting checkbox ref and checking checkbox state

I have a React component which is outputting a list of checkboxes. I'm trying to set a ref for each checkbox with an ID which is pulled from the handleChange function. I would like to name the ref "id - checkbox" using the id variable which is being successfully collected by networkService:

networkService = ({id, label, service_state, active_path = [], z_end = []} = {}) => (
<tr>
  <td>
    <form>
      <input type="checkbox" ref="REF HERE WITH id-checkbox" onChange={() => this.handleChange(`${id}`)} />
    </form>
  </td>      
</tr>

)

I have tried numerous iterations of ${id} - checkbox as in ref=${id} - checkbox but am getting errors.

Furthermore, in another function I need to check the state of that checkbox via the ref, and again the syntax is beyond me. Here is the function:

 handleChange = (id) => {

     if (this.refs.CHECKBOX REF HERE.checked) {

The id is coming successfully from the onChange call shown above.

Any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire