javascript 比较React引用以设置类名

nue99wik  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(90)

所以我尝试在React中聚焦输入时设置一个className。我用的是裁判。我也在使用React v16.8.3。
在输入className处,我正在比较聚焦的ref和聚焦的输入。我添加了一个测试函数,当我在这个函数中比较它时,它工作得很好。但由于某些原因,我不明白为什么它在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;
nafvub8i

nafvub8i1#

我把它修好了。我把函数组件转换成了一个类组件,让它像这样工作:

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;
fnvucqvd

fnvucqvd2#

onFocus更改状态,并导致组件重新渲染和更改引用。className渲染期间检查错误的引用test在渲染后检查正确的引用。
尝试在return之前、ref设置内部和onFocus事件内部调用test

相关问题