Q
Q
Quintis2019-12-14 00:30:16
JavaScript
Quintis, 2019-12-14 00:30:16

Why doesn't js DOM work in react?

Good day, tell me why it is impossible to pick up all classes from the list elements through js DOM? And how to highlight clicked element in react? Only through ref ?

class MyComponent extends Component {
  click=(e)=>{
     document.getElementsByTagName('li').classList.remove("active")

    e.target.className = "active"
  }

  render() {
      return (
          <ul>
              <li onClick={(e)=>this.click(e)}>item1</li>
              <li onClick={(e)=>this.click(e)}>item2</li>
              <li onClick={(e)=>this.click(e)}>item3</li>
          </ul>
      )
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2019-12-14
@Quintis

Such things are done in react not directly through work with the house, but through work with the internal state of the component. Read the documentation, take a training course about tic-tac-toe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question