如何使用React.createRef()访问react中的Dom的多个元素

问题描述 投票:1回答:1

我知道如何使用React.createRef()访问dom的单个元素。但是我想使用createRef访问两个不同的元素。我已经看到了一些关于stackoverflow的示例,该示例可以动态访问多个元素但无法理解。我在这里附加了我的简单代码,我想在其中更改span标签onClick的背景颜色。接下来,我对React Docs进行了反应。有人可以指导我,我在这里做错了什么。

class TodoApp extends React.Component {
  constructor(props) {
    super(props)
    // this.myRef = React.createRef();
    this.textInput = null;

    this.state = {
      fname:""
    }
  }

  setTextInputRef = element => {
    console.log("element",element);
    this.textInput = element;
  };

green = () =>{
  console.log("green ",this.textInput);
  /*   this.textInput.current.style.backgroundColor = "green"; */
  this.textInput.style.backgroundColor = "green";
}

red = () =>{
  console.log("red ",this.textInput);
  /*   this.textInput.current.style.backgroundColor = "red"; */
  this.textInput.style.backgroundColor = "red";
}

  render() {
    return (
      <div>
      {/* 
      <span id="green" ref={input => { this.setTextInputRef = input; }}>Green</span><br/>
      <span id="red" ref={input => { this.setTextInputRef = input; }}>Red</span><br/> 
      */}
      <span id="green" ref={this.setTextInputRef}>Green</span><br/>
      <span id="red" ref={this.setTextInputRef}>Red</span><br/>
      <button onClick={this.green}>For Green</button><br/>
      <button onClick={this.red}>For Red</button><br/>
      </div>
    )
  }
}

ReactDOM.render(<TodoApp />, document.querySelector("#app"))

在上面的代码中,如果用户单击btn,则应更改相应的跨度颜色。

任何帮助将不胜感激。

here is jsfiddle for more info

javascript reactjs dom-events ref create-ref
1个回答
0
投票

如果渲染的元素是静态的,则可以在构造函数中创建多个引用,并使用this.myRef.current访问这些元素>

您的带有createRef的代码如下所示

import React from "react";
import "./styles.css";

export default class TodoApp extends React.Component {
  constructor(props) {
    super(props);
    this.greenInputRef = React.createRef();
    this.redInputRef = React.createRef();
    this.state = {
      fname: ""
    };
  }

  green = () => {
    console.log("green ", this.textInput);
    /*   this.textInput.current.style.backgroundColor = "green"; */
    this.greenInputRef.current.style.backgroundColor = "green";
  };

  red = () => {
    console.log("red ", this.textInput);
    /*   this.textInput.current.style.backgroundColor = "red"; */
    this.redInputRef.current.style.backgroundColor = "red";
  };

  render() {
    return (
      <div>
        {/* 
      <span id="green" ref={input => { this.setTextInputRef = input; }}>Green</span><br/>
      <span id="red" ref={input => { this.setTextInputRef = input; }}>Red</span><br/> 
      */}
        <span id="green" ref={this.greenInputRef}>
          Green
        </span>
        <br />
        <span id="red" ref={this.redInputRef}>
          Red
        </span>
        <br />
        <button onClick={this.green}>For Green</button>
        <br />
        <button onClick={this.red}>For Red</button>
        <br />
      </div>
    );
  }
}

Working Demo

如果要通过循环动态呈现代码段,则可以按以下方式创建引用

constructor(props) {
   super(props);

   props.data.forEach(item => {
      this[`dataRef${item.id}] = React.createRef();
   })
}  

...
render() {
   return (
       <div>
        {
           data.map(item => 
              <p ref={this[`dataRef{item.id}]}>{item.name}</p>
           )
         }
       </div>
   )
}
© www.soinside.com 2019 - 2024. All rights reserved.