简体   繁体   English

带有 id 的声明元素并在同一个 JSX 中访问它

[英]Declaration element with id and access it in same JSX

I have dynamically created JSX and 1 element is getting its ID dynamically:我已经动态创建了 JSX 并且 1 个元素正在动态获取其 ID:

{props.types[2].map((element, index) => {
            return (
              <tr key={index}>
                <td className="align-middle">
                  {element.Type &&
                    commonFuctions.splitCapitalLetterString(element.Type)}
                </td>
                <td className="align-middle">
                  <div className="slideThree">
                    <input
                      type="checkbox"
                      value="None"
                      id={element.Type}
                      name="check"
                    />
                    <label htmlFor={element.Type}></label>
                  </div>
                ....
                ....
                ....

As you see the element which is getting dynamically id property is the checkbox.如您所见,动态获取 id 属性的元素是复选框。 Below in the same JSX I have:下面在我有的同一个 JSX 中:

{/**********************PENSION TYPES***************************************** */}
      {props.types[0].length > 0
        ? DataExtract.uniquePensionTypes(props.types[0]).map(
            (element, index) => {
              {
                console.log(document.getElementById(`${element}`));
              }
            }
          )
        : ""}
    </div>

The important part here is this console.log.这里的重要部分是这个 console.log。 Argument in getElementById is the same as the id for checkbox. getElementById 中的参数与复选框的 id 相同。 But for some reason it is not discovering the element and it is displaying null.但由于某种原因,它没有发现该元素,而是显示为空。 So my questions is why?所以我的问题是为什么? Is it at all possible to declare id of element and below in the same jsx to access it?是否有可能在同一个 jsx 中声明元素的 id 和下面的元素来访问它? I even tried to put static id for this checkbox and below to reach it with it's static value but still not working.我什至试图为这个复选框和下方放置静态 id 以使用它的静态值到达它,但仍然无法正常工作。

We cannot access the dom elements in the render method.我们无法访问 render 方法中的 dom 元素。 Render just renders the changes on the Virtual DOM and not on the actual DOM. Render 只是在虚拟 DOM 上呈现更改,而不是在实际 DOM 上。

If you want to access the elements rendered on DOM, do the same in ComponentDidMount or ComponentDidUpdate .如果要访问 DOM 上呈现的元素,请在ComponentDidMountComponentDidUpdate执行相同操作。

You could use React ref api to access the dom.您可以使用React ref api 来访问 dom。

Refs provide a way to access DOM nodes or React elements created in the render method. Refs 提供了一种访问 DOM 节点或在 render 方法中创建的 React 元素的方法。

The document.getElementById is displaying null because the html element is not created yet. document.getElementById显示为null因为尚未创建 html 元素。 It's not recommended to directly access DOM elements in React .不建议在React直接访问DOM元素。

Example:例子:

Class component类组件

import React, {Component} from 'react';

class MyComponent extends Component {

  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }

  render() {
    return (
      <table>
        {props.types[2].map((element, index) => {
          return (
            <tr key={index}>
              <td className="align-middle">
                {element.Type &&
                  commonFuctions.splitCapitalLetterString(element.Type)}
              </td>
              <td className="align-middle">
                <div className="slideThree">
                  <input
                    type="checkbox"
                    value="None"
                    id={element.Type}
                    name="check"
                    ref={this.myRef}
                  />
                  <label htmlFor={element.Type}></label>
                </div>
              </td>
            </tr>
          );
        })}
      </table>
    );
  }
}

Functional Component功能组件

import React, {useRef} from 'react'

function SomeComponent(props) {
  const inputEl = useRef(null);

  return (
    <table>
      {props.types[2].map((element, index) => {
        return (
          <tr key={index}>
            <td className="align-middle">
              {element.Type &&
                commonFuctions.splitCapitalLetterString(element.Type)}
            </td>
            <td className="align-middle">
              <div className="slideThree">
                <input
                  type="checkbox"
                  value="None"
                  id={element.Type}
                  name="check"
                  ref={inputEl}
                />
                <label htmlFor={element.Type}></label>
              </div>
            </td>
          </tr>
        );
      })}
    </table>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM