【问题标题】:How to make FAQ section in reactJs?如何在 reactJs 中制作常见问题解答部分?
【发布时间】:2023-01-12 23:59:32
【问题描述】:
` I am trying to make FAQ section but problem here with my code is when i am clicking open(+)
 Button of particular section it will open all the section and same case with close(-) button 
 also.`

        ```
         export default function App() {
         const [isOpen,setIsOpen] = useState(false)
          return (
      <div className="App">
        <h1>Faq/Accordian</h1>
      <div className="accordian">
       {questions.map((ele)=>(
        <div key={ele.id} className="faq">
         <h3>{ele.title}</h3>
         <button onClick={() => setIsOpen(!isOpen)}>{isOpen ? "-" : "+"}</button>
        {isOpen===true && <h4>{ele.info}</h4>}
         </div>
         ))}
        </div>
       </div>
     );
     }

     ```

我期待当我点击特定的打开按钮时它应该打开那个特定的部分

【问题讨论】:

    标签: javascript reactjs toggle


    【解决方案1】:

    您可以将选定的索引置于状态。

    export default function App() {
        const [idx, setIdx] = useState();
        return (
          <div className="App">
            <h1>Faq/Accordian</h1>
            <div className="accordian">
            {questions.map((ele, i)=>(
                <div key={ele.id} className="faq">
                 <h3>{ele.title}</h3>
                 <button onClick={() => setIdx(i === idx ? undefined : i)}>{i === idx ? "-" : "+"}</button>
                {i === idx && <h4>{ele.info}</h4>}
                 </div>
             ))}
            </div>
           </div>
         );
     }
    

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 2022-11-11
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2021-10-16
      相关资源
      最近更新 更多