You can use Callback Refs to retrieve the ID:
class YourComponent extends React.Component {
constructor(props) {
super(props)
this.canvas = null
}
componentDidMount() {
console.log(this.canvas.id) // gives you "myCanvas"
}
render() {
return (
<React.Fragment>
<div id="animSec">
<canvas id="myCanvas" ref={c => {this.canvas = c}}></canvas>
</div>
</React.Fragment>
)
}
}
alternatively, for React v16.3+, you can use createRef():
constructor(props) {
super(props)
this.canvas = React.createRef()
}
componentDidMount() {
console.log(this.canvas.current.id) // gives you "myCanvas"
}
render() {
return (
<React.Fragment>
<div id="animSec">
<canvas id="myCanvas" ref={this.canvas}></canvas>
</div>
</React.Fragment>
)
}
If you set up a ref in your constructor as this.canvasRef = React.createRef() and apply it to your canvas as
<React.Fragment>
<div id="animSec">
<canvas ref={this.canvasRef}></canvas>
</div>
</React.Fragment>
You should be able to access the element directly. You can console log or check your dev tools to view the ref value. And (to my best knowledge), it's best practice to use Refs compared to querySelectors in React. You might also check out this post to see if you can work around with a method on canvas.
I'd like to download a protected file from my backed - I have to send authorization headers, so I can't link it directly. I have created following Ajax request to download it: Ember.$.ajax({ url:...
I'd like to download a protected file from my backed - I have to send authorization headers, so I can't link it directly. I have created following Ajax request to download it: Ember.$.ajax({ url:...
I'm looking for an operator which transform the stream to the stream with history or last N events. So for a stream e.g. like this one: clicks$.subscribe((ev) => ...) I would like to have ...
I'm looking for an operator which transform the stream to the stream with history or last N events. So for a stream e.g. like this one: clicks$.subscribe((ev) => ...) I would like to have ...
I have one static component and many for loop rendered components in a screen. The all code below here. import React from "react"; import { View, Text, FlatList } from "react-native"; ... class ...
I have one static component and many for loop rendered components in a screen. The all code below here. import React from "react"; import { View, Text, FlatList } from "react-native"; ... class ...
I have a basic script which shows/hides a div. I'm using this for a drop-down menu. https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp I'm looking for the div element to be hidden when ...
I have a basic script which shows/hides a div. I'm using this for a drop-down menu. https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp I'm looking for the div element to be hidden when ...