Open matthewdking opened 4 years ago
Good advice. We didn't cover refs, but they're a way to get a reference to a DOM element without having to query the DOM directly.
Meta code-review: you probably want the ref={audioRef}
on the <audio>
, not the <source>
Ooh yeah great spot @oliverjam will edit
In React you are using a virtual DOM that React uses to render updates quickly. I would suggest using the virtual DOM at all times, this means not accessing the actual DOM using APIs such as
document.querySelector
. When using something likedocument.querySelector
it will search at a DOM level rather than component level and therefore could return unexpected results.This may not be a problem here but in a bigger codebase so I thought it worth mentioning. There is an alternative...
Using refs (see the docs) would solve this issue.
The following is semi pseudo code I didn't check it would run 🙃