Эх сурвалжийг харах

Handling copy game ID logic w/ proper React ref

Kirk Trombley 5 жил өмнө
parent
commit
09568b4152

+ 9 - 5
client/src/components/pre-round.component.jsx

@@ -1,11 +1,14 @@
 import React from "react";
 
 class PreRound extends React.Component {
+  constructor(props) {
+    super(props);
+    this.textareaRef = React.createRef();
+  }
+
   copyGameId() {
-    const { gameId } = this.props;
-    const textarea = this.textarea;
-    textarea.value = gameId;
-    textarea.select();
+    const { current } = this.textareaRef;
+    current.select();
     document.execCommand("copy");
   }
   
@@ -19,8 +22,9 @@ class PreRound extends React.Component {
         </p>
         <button onClick={onStart}>Start Game</button>
         <textarea
-          ref={textarea => this.textarea = textarea}
+          ref={this.textareaRef}
           style={{ position: "absolute", top: "-1000px" }}
+          value={gameId}
         />
       </div>
       );