React 中动态生成的组件的引用?
Refs for dynamically generated components in React?
下面的代码是解释我的问题的最小工作示例。此代码使用 Array.map 生成 3 个 Note 组件,当您在其中按下回车键时,它会使用对其 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 组件存储一个单独的 ref,然后将焦点中的 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;
下面的代码是解释我的问题的最小工作示例。此代码使用 Array.map 生成 3 个 Note 组件,当您在其中按下回车键时,它会使用对其 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 组件存储一个单独的 ref,然后将焦点中的 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;