如何在使用 React.createRef() 的反应中访问 Dom 的多个元素
How to access more than one element of Dom in react using React.createRef()
我知道如何使用 React.createRef() 访问 dom 的单个元素。但是我想使用 createRef 访问两个不同的元素。我在 Whosebug 上看到了一些用于动态访问多个元素但无法 understand.I 的示例,我在此处附加了我的简单代码,我想在其中更改 span 标签的背景颜色 onClick of button。
我通过 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,则应更改相应的跨度颜色。
我们将不胜感激。
如果呈现的元素是静态的并使用 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>
);
}
}
如果您通过循环动态呈现代码片段,您可以按以下方式创建 ref
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>
)
}
我知道如何使用 React.createRef() 访问 dom 的单个元素。但是我想使用 createRef 访问两个不同的元素。我在 Whosebug 上看到了一些用于动态访问多个元素但无法 understand.I 的示例,我在此处附加了我的简单代码,我想在其中更改 span 标签的背景颜色 onClick of button。 我通过 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,则应更改相应的跨度颜色。
我们将不胜感激。
如果呈现的元素是静态的并使用 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>
);
}
}
如果您通过循环动态呈现代码片段,您可以按以下方式创建 ref
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>
)
}