gpt4 book ai didi

javascript - 比较 React refs 以设置类名

转载 作者:行者123 更新时间:2023-12-03 00:09:00 24 4
gpt4 key购买 nike

因此,当输入集中在 React 中时,我尝试设置一个 className。我正在为此使用 ref 。我也在使用 React v16.8.3。

在输入 className 处,我将焦点引用与焦点输入进行比较。我添加了一个测试函数,当我在这个函数中比较它时,它工作得很好。但由于某种原因,我不明白为什么它在 className if/else 中不起作用。

我希望我想做什么有点清楚。

import React, {useState} from 'react';

function EditSocialmedia (props) {

const [facebook, setFacebook] = useState(props.social.youtube);
const [youtube, setYoutube] = useState(props.social.youtube);
const [twitter, setTwitter] = useState(props.social.twitter);
const [focusedElement, setFocusedElement] = useState(React.createRef());
let refFacebook = React.createRef();
let refYoutube = React.createRef();
let refTwitter = React.createRef();

function test () {
console.log(focusedElement)
console.log(refFacebook === focusedElement)
console.log(refYoutube === focusedElement)
console.log(refTwitter === focusedElement)
}

return (
<div>
<h2 className="hSmaller hthin">Social Media</h2>

<input id={`facebook`}
className={refFacebook === focusedElement ? "active" : ""}
ref={(input) => refFacebook = input}
value={facebook ? facebook : ""}
onChange={() => {setFacebook(refFacebook.value)}}
onFocus={() => setFocusedElement(refFacebook)}/>

<input id={`youtube`}
className={focusedElement === refYoutube ? "active" : ""}
ref={(input) => refYoutube = input}
value={youtube ? youtube : ""}
onChange={() => {setYoutube(refYoutube.value)}}
onFocus={() => setFocusedElement(refYoutube)}/>

<input id={`twitter`}
className={focusedElement === refTwitter ? "active" : ""}
ref={(input) => refTwitter = input}
value={twitter ? twitter : ""}
onChange={() => {setTwitter(refTwitter.value)}}
onFocus={() => setFocusedElement(refTwitter)}/>

<div onClick={test}>Test</div>
</div>
)
}

export default EditSocialmedia;

最佳答案

我成功了。我将函数组件转换为类组件,让它像这样工作:

import React, {Component} from 'react';

class EditSocialmedia extends Component {

constructor(props) {
super(props);
this.state = {
facebook: "",
youtube: "",
twitter: "",
focusedElement: React.createRef()
};
}

render () {
const {facebook, youtube, twitter} = this.props.social
return (
<div>
<h2 className="hSmaller hthin">Social Media</h2>

<input id={`facebook`}
className={this.refFacebook === this.state.focusedElement ? "active" : ""}
ref={(input) => this.refFacebook = input}
value={facebook ? facebook : ""}
onChange={() => {this.setState({facebook: this.refFacebook})}}
onFocus={() => this.setState({focusedElement: this.refFacebook})}/>

<input id={`youtube`}
className={this.refYoutube === this.state.focusedElement ? "active" : ""}
ref={(input) => this.refYoutube = input}
value={youtube ? youtube : ""}
onChange={() => {this.setState({youtube: this.refYoutube})}}
onFocus={() => this.setState({focusedElement: this.refYoutube})}/>

<input id={`twitter`}
className={this.refTwitter === this.state.focusedElement ? "active" : ""}
ref={(input) => this.refTwitter = input}
value={twitter ? twitter : ""}
onChange={() => {this.setState({twitter: this.refTwitter})}}
onFocus={() => this.setState({focusedElement: this.refTwitter})}/>

</div>
)
}

}

export default EditSocialmedia;

关于javascript - 比较 React refs 以设置类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54827264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com