1

I'm trying to use the javascript function .checked() in React.js like this :

componentDidMount () {
    document.getElementById("myCheck").checked = true;
}

But I have an error in my console : "la propriété 'checked' n'existe pas sur le type HTMLlement"

How can I use this in React.js ?

Thank you

Anaïs Saez
  • 1,259
  • 5
  • 20
  • 38
  • In React you have to update the state to re-render the component. Please post more code. – Dev Aug 23 '17 at 09:45
  • 4
    why you want to do it like this? react provides a better way of controlling elements, simply use `` for [controlled component](https://facebook.github.io/react/docs/forms.html), if you want to use [uncontrolled component](https://facebook.github.io/react/docs/uncontrolled-components.html) then use ` ` – Mayank Shukla Aug 23 '17 at 09:46
  • I would rely on your component state for the checkbox status, instead of modifying any properties of the DOM by hand. Take a look to [this](https://stackoverflow.com/questions/32174317/how-to-set-default-checked-in-checkbox-react-js) question and answer, since it handles a really similar problem. – andersuamar Aug 23 '17 at 09:50
  • 1
    @MayankShukla Thank you for your answer , it was very simple .. and I didn't think about it ! Thank you – Anaïs Saez Aug 23 '17 at 09:57

1 Answers1

1

It worked for me

<div className='container'>
    <input type='checkbox' ref='myAwesomeCheckbox' id={'scales'} />
    <label fhtmlForor="scales">Scales</label>
</div>

componentDidMount = () => {
  this.refs.myAwesomeCheckbox.checked = true;
}