React 切换按钮点击时的 div 可见性

huangapple go评论70阅读模式
英文:

React toggle div visibility on button click

问题

我想要在按钮点击时切换div内容的可见性。为此,我正在使用UseId钩子,如下所示:

function toggleElement(elm_id) {
  var el = document.getElementById(elm_id).nextElementSibling;
  if (el.style.display === "block") {
    el.style.display = "none";
  } else {
    el.style.display = "block";
  }
}

function FAQ(props) {
  const clickedElm = useId();
  return (
    <div className="faq-item">
      <button type="button" id={clickedElm} class="collapsible" onClick={toggleElement(this.id)}>
        {props.question}
      </button>
      <div className="content">
        <p>{props.answer}</p>
      </div>
    </div>
  );
}

上面的代码显示了“未使用错误边界”。我是React的初学者,我不明白这是什么意思。代码哪里出错了?

英文:

I want to toggle the visibility of the div content on button click. For this, I'm using UseId hook as below

function toggleElement(elm_id) {
  var el = document.getElementById(elm_id).nextElementSibling;
      if (el.style.display === &quot;block&quot;) {
        el.style.display = &quot;none&quot;;
      } else {
        el.style.display = &quot;block&quot;;
      }
   
}
function FAQ(props) {
  const clickedElm = useId();
  return (
    &lt;div className=&quot;faq-item&quot;&gt;
      &lt;button type=&quot;button&quot; id = {clickedElm} class=&quot;collapsible&quot; onClick={toggleElement(this.id)} &gt;
        {props.question}
      &lt;/button&gt;
      &lt;div className=&quot;content&quot;&gt;
        &lt;p&gt;{props.answer}&lt;/p&gt;
      &lt;/div&gt;

The above code is showing Error Boundaries not used error. I'm beginner to React. I didn't understand what it is. Where the code is going wrong?

答案1

得分: 3

在React中,直接操作DOM是不被鼓励的,就像您在这里所做的一样:

if (el.style.display === "block") {
  el.style.display = "none";
} else {
  el.style.display = "block";
}

因为React有其自己的内部表示来确定何时更新以及何时不更新等等。

相反,React的做法是像这样做你想要的事情:

import React from 'react';
import './style.css';

export default function App() {
  let [show, setShow] = React.useState(false);
  return (
    <div>
      <button
        onClick={() => {
          setShow(!show);
        }}
      >
        点击
      </button>
      {show && <div>你好</div>}
    </div>
  );
}
英文:

It is discouraged in react to touch the DOM directly like you do here:

 if (el.style.display === &quot;block&quot;) {
        el.style.display = &quot;none&quot;;
      } else {
        el.style.display = &quot;block&quot;;
      }

because react has its own internal representation to determine what to update what not, etc.

Instead react way of doing something like what you want is this:

import React from &#39;react&#39;;
import &#39;./style.css&#39;;

export default function App() {
  let [show, setShow] = React.useState(false);
  return (
    &lt;div&gt;
      &lt;button
        onClick={() =&gt; {
          setShow(!show);
        }}
      &gt;
        Click
      &lt;/button&gt;
      {show &amp;&amp; &lt;div&gt;Hi&lt;/div&gt;}
    &lt;/div&gt;
  );
}

答案2

得分: 0

我已经解决了这个问题。请查看下面的代码:

function toggleElement(elm_id) {
    var el = elm_id.currentTarget.nextElementSibling;
    if (el.style.display === "block") {
        el.style.display = "none";
    } else {
        el.style.display = "block";
    }
}

function FAQ(props) {
    return (
        <div className="faq-item">
            <button type="button" id='btntest' class="collapsible" onClick={toggleElement}>
                {props.question}
            </button>
            <div className="content">
                <p>{props.answer}</p>
            </div>
        </div>
    )
}

这里不需要使用 UseId 钩子,并且我只是更新了你的一些代码行。尝试这个,让我知道是否对你有用。

英文:

I have solved this issue. please check below code :-

    function toggleElement(elm_id) {
        var el = elm_id.currentTarget.nextElementSibling;
            if (el.style.display === &quot;block&quot;) {
              el.style.display = &quot;none&quot;;
            } else {
              el.style.display = &quot;block&quot;;
            }
    }
    function FAQ(props) {
        return (
           &lt;div className=&quot;faq-item&quot;&gt;
              &lt;button type=&quot;button&quot; id=&#39;btntest&#39; class=&quot;collapsible&quot; onClick={toggleElement}&gt;
                {props.question}
              &lt;/button&gt;
              &lt;div className=&quot;content&quot;&gt;
                 &lt;p&gt;{props.answer}&lt;/p&gt;
              &lt;/div&gt;
           &lt;/div&gt;
        )
    }

There is no need to use UseId hook here and I have just updated some line of your code. Try this out and let me know if it works for you.

huangapple
  • 本文由 发表于 2023年1月9日 01:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定