How should I structure this ReactJS code to use ReactDOM / getDOMNode()
instead of jQuery. This plays a sound when a button is pressed.
Not sure if I should put the .play()
in myAudioObject
and access it via Refs, or the other way... pass Refs with .getDomNode()
into myPlayButton
.
Also, is using jQuery they way i have it below considered bad practice with ReactJS?
var myAudioObject = React.createClass({
render: function() {
return (
<audio id="mySoundClip" preload="auto">
<source src='audio/thing.mp3'></source>
Your browser does not support audio.
</audio>
);
}
});
var myPlayButton = React.createClass({
handleClickPlay: function() {
var audio = $("#mySoundClip")[0];
audio.load(); // This reloads the audio tag in the DOM, so also reloads ReactJS component.
audio.play();
},
render: function() {
return (
<button onClick={this.handleClickPlay}>Play</button>
);
}
});
var App = React.createClass({
render: function() {
return (
<div>
<myAudioObject />
<myPlayButton />
</div>
);
}
});
React.renderComponent((
<App />
), document.body);
To solve the error, create a root element and use the ReactDOMClient. render method instead. Copied! import {StrictMode} from 'react'; import {createRoot} from 'react-dom/client'; import App from './App'; // 👇️ IMPORTANT: use correct ID of your root element // this is the ID of the div in your index.
The ReactDOM. render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.
In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.
render() The first argument is the element or component we want to render, and the second argument is the HTML element (the target node) to which we want to append it. Generally, when we create our project with create-react-app , it gives us a div with the id of a root inside index.
You're on the right track, try using refs like this (or use flux) - JSFiddle
var myAudioObject = React.createClass({
play: function () {
var audio = this.getDOMNode();
audio.load();
audio.play();
},
render: function() {
return (
<audio preload="auto">
<source src='audio/thing.mp3'></source>
Your browser does not support audio.
</audio>
);
}
});
var myPlayButton = React.createClass({
render: function() {
return (
<button onClick={this.props.handleClickPlay}>Play</button>
);
}
});
var App = React.createClass({
handleClickPlay: function() {
this.refs.audioObject.play()
},
render: function() {
return (
<div>
<myAudioObject ref="audioObject" />
<myPlayButton handleClickPlay={this.handleClickPlay} />
</div>
);
}
});
using jQuery like that is frowned upon, because it quickly becomes unclear whats depending on what.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With