Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactDOM / getDOMNode() instead of jQuery?

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);
like image 605
Giant Elk Avatar asked Sep 23 '14 03:09

Giant Elk


People also ask

Why ReactDOM Render is not working?

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.

What is ReactDOM render in React JS?

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.

Can I use dom in React?

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.

Which component do we have to pass to the ReactDOM render () method?

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.


1 Answers

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.

like image 72
Chad Scira Avatar answered Oct 27 '22 17:10

Chad Scira