javascript - React 中动态生成组件的引用?

标签 javascript reactjs dom ref

下面的代码是解释我的问题的最小工作示例。此代码使用 Array.map 生成 3 个 Note 组件,当您在其中按 Enter 时,它会使用对其 DOM 元素的引用来清空它们上面静态生成的 Note 组件。

我想做的是将您按回车键的注释本身设为空。我认为这需要为每个 Note 生成一个包含 {id, ref} 的动态数组,因此我可以将 Note id 传递给handleKeyDown,然后在 refs 数组中搜索该 id 并使用相应的 ref 来更改 DOM 元素。但我无法弄清楚如何实际做到这一点。

我意识到在这里使用 refs 是没有必要的,但这只是一个示例,因为我的实际代码要长得多并且调用 focus()。

import React, { Component } from "react";
import "./App.css";

class App extends Component {
  constructor() {
    super();
    this.notes = [
      { text: "Hello world 1", id: 1 },
      { text: "Hello world 2", id: 2 },
      { text: "Hello world 3", id: 3 }
    ];
    this.ref = React.createRef();
  }

  handleKeyDown = event => {
    if (event.key === "Enter") {
      event.preventDefault();
      this.ref.current.textContent = "";
    }
  };

  render() {
    return (
      <div>
        <Note
          text={"StaticNote"}
          key={0}
          id={0}
          handleKeyDown={this.handleKeyDown}
          innerRef={this.ref}
        />
        {this.notes.map(note => (
          <Note
            text={note.text}
            key={note.id}
            id={note.id}
            handleKeyDown={this.handleKeyDown}
          />
        ))}
      </div>
    );
  }
}

class Note extends Component {
  render() {
    return (
      <p
        ref={this.props.innerRef}
        contentEditable
        onKeyDown={this.props.handleKeyDown}
      >
        {this.props.text}
      </p>
    );
  }
}

export default App;

最佳答案

您需要为所有 Note 组件存储一个单独的引用,然后将焦点中的 Note 的索引传回给 handleKeyDown 函数

import React, { Component } from "react";
import "./App.css";

class App extends Component {
  constructor() {
    super();
    this.notes = [
      { text: "Hello world 1", id: 1, ref: React.createRef() },
      { text: "Hello world 2", id: 2, ref: React.createRef() },
      { text: "Hello world 3", id: 3, ref: React.createRef() }
    ];
  }

  handleKeyDown = (event, index) => {
    if (event.key === "Enter") {
      event.preventDefault();
      this.notes[index].ref.current.textContent = "";
    }
  };

  render() {
    return (
      <div>
        <Note
          text={"StaticNote"}
          key={0}
          id={0}
          handleKeyDown={this.handleKeyDown}
          innerRef={this.ref}
        />
        {this.notes.map((note, index) => (
          <Note
            index={index}
            innerRef = {note.ref}
            text={note.text}
            key={note.id}
            id={note.id}
            handleKeyDown={this.handleKeyDown}
          />
        ))}
      </div>
    );
  }
}

class Note extends Component {
  render() {
    return (
      <p
        ref={this.props.innerRef}
        contentEditable
        onKeyDown={(e) => this.props.handleKeyDown(this.props.index)}
      >
        {this.props.text}
      </p>
    );
  }
}

export default App;

关于javascript - React 中动态生成组件的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53368828/

相关文章:

javascript - 将 html bootstrap 转换为 react.js jsx 样式后更改 css 样式

javascript - 使用 $element.each() 加速 DOM 访问

javascript - 如何循环遍历字母数组并返回数组中的第一个连续辅音?

node.js - 使用 Express 设置 React Router

javascript - flex 布局内可调整大小的窗口需要 SVG 滚动条

javascript - Regex jQuery find ("option:[text^=' "]) 在 ie 8 和 chrome 上出现错误

javascript - DOMParser parseFromString 仅在迭代结果主体 childNodes 时删除 Node

javascript - 使用 CSS 和 Javascript 制作多个数据 'visible' 或 'hidden'

javascript - 如何舍入存储在变量中的两个函数返回值的总和?

javascript - 在 Azure Functions Typescript 中使用 IBinder?