简体   繁体   English

在React中按ID访问DOM元素

[英]Access to DOM elements by ID in React

I want to handle disable attribute of input elements by ID in my React app 我想在我的React应用程序中通过ID处理输入元素的禁用属性

It's possible with states but number of elements is not fixed, maybe 10 input or 20 or more ... 状态是可能的,但是元素的数量不是固定的,可能是10个输入或20个或更多...

I've decided to set ID for each input and access to them with ID, for example : 我决定为每个输入设置ID并使用ID来访问它们,例如:

 document.getElementById('title-12') .... 

So, is it a suitable trick or best practice to handle this issue ? 那么,处理此问题是否合适?

Performance and clean code is very important for me :-) 性能和简洁的代码对我来说非常重要:-)

Thanks 谢谢

Oops... my bad. 糟糕...我的糟糕。 I digged into your discussion and here's a new solution. 我深入研究了您的讨论,这是一个新的解决方案。

That's still correct that React approach is advised so we should use reusable components for inputs (we can have any number of inputs right now). 建议使用React方法还是正确的,因此我们应该对输入使用可重用组件(我们现在可以有任意数量的输入)。 All the input's data are stored in parent component's store as a collection. 所有输入的数据都作为集合存储在父组件的存储中。 We map through collection and send properties to each component (in the simplest version - id , isDisabled and disableInput() function). 我们通过集合映射并将属性发送到每个组件(在最简单的版本中idisDisableddisableInput()函数)。

class Titles extends Component {
  constructor(props) {
    super(props);
    this.state = {
      titles: [
        {
          id: 0,
          disabled: true
        },
        {
          id: 1,
          disabled: false
        }
      ]
    };
  }

  addNewInput = () => {
    const prevList = this.state.titles;
    const newItem = {
      id: prevList.length,
      disabled: false
    };
    this.setState({ titles: [...prevList, newItem] });
  };

  disableInput = id => {
    const titles = this.state.titles;
    titles[id].disabled = !titles[id].disabled;
    this.setState({ titles });
  };

  render() {
    return (
      <div>
        <h1>Titles list</h1>
        <form style={{ display: "flex", flexDirection: "column" }}>
          {this.state.titles.map(title => (
            <Title
              key={title.id}
              id={title.id}
              isDisabled={title.disabled}
              disableInput={id => this.disableInput(id)}
            />
          ))}
        </form>
        <button onClick={() => this.addNewInput()}>Dodaj nowy</button>
      </div>
    );
  }
}

in Title component we just render the props in to <input> and a button with onClick function that sends id of this component to its parent, where disable attribute's value is being reversed. 在“ Title组件中,我们只是将道具渲染到<input>并使用一个带有onClick函数的按钮,该按钮将该组件的id发送给其父级,在该父级中,禁用属性的值被反转。

const Title = ({ id, isDisabled, disableInput }) => {
  return (
    <div>
      <input
        id={id}
        type="text"
        placeholder={id}
        disabled={isDisabled}
      />
      <button type="button" onClick={() => disableInput(id)}>
        disable input
      </button>
    </div>
  );
};

Working example can be found here. 工作示例可以在这里找到。

Please let me know if it works for you. 请让我知道它是否适合您。

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

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