简体   繁体   English

在 React 中为 FAQ 切换获取“TypeError: cannot read classList of undefined”

[英]Getting "TypeError: cannot read classList of undefined" for FAQ toggle in React

How do I alter this JS so it works inside of a react component?如何更改此 JS 使其在 React 组件内工作? When I add an onClick handler to the span button I get "cannot read property classList of undefined".当我向 span 按钮添加 onClick 处理程序时,我得到“无法读取未定义的属性 classList”。 This was JS taken out of a non react project so I understand that I'm probably not doing this as best practice, but need the direction to make it work.这是从非反应项目中取出的 JS,所以我知道我可能没有将其作为最佳实践,但需要指导才能使其发挥作用。

Here is my code.这是我的代码。

 import React from "react" const Faq = () => { const toggleFAQ = function(question) { if( question.parentElement.classList.contains('is-opened') ) { question.parentElement.classList.remove('is-opened'); } else { question.parentElement.classList.add('is-opened'); } } if( document.querySelector('.faq') !== null ) { const questions = Array.from( document.getElementsByClassName('faq__question') ); questions[0].parentElement.classList.add('is-opened'); questions.forEach((question) => { question.addEventListener('click', (e) => toggleFAQ(question)); }) } return ( <> <section className="faq"> <div className="faq__inner"> <h2 className="faq__title">Frequently Asked Questions</h2> <ul className="faq__list"> <li className="faq__item"> <h3 className="faq__question"> How do I make this work? <span className="faq__question-btn" onClick={toggleFAQ}></span> </h3> <article className="faq__answer is-opened"> <div className="faq__answer-inner is-opened"> <p> Lorem Ipsum </p> </div> </article> </li> <li className="faq__item"> <h3 className="faq__question"> Help me, Lord? <span className="faq__question-btn"></span> </h3> <article className="faq__answer"> <div className="faq__answer-inner"> <p> You suck at JS </p> </div> </article> </li> <li className="faq__item"> <h3 className="faq__question"> How does this work. Help? <span className="faq__question-btn"></span> </h3> <article className="faq__answer"> <div className="faq__answer-inner"> <p> Lorem Ipsum. </p> </div> </article> </li> <li className="faq__item"> <h3 className="faq__question"> Lorem Ipsum? <span className="faq__question-btn"></span> </h3> <article className="faq__answer"> <div className="faq__answer-inner"> <p> Lorem ipsum </p> </div> </article> </li> <li className="faq__item"> <h3 className="faq__question"> Lorem Ipsum? <span className="faq__question-btn"></span> </h3> <article className="faq__answer"> <div className="faq__answer-inner"> <p> Lorem Ipsum </p> </div> </article> </li> </ul> </div> </section> </> ) } export default Faq

Thanks in advance!提前致谢!

In React, you don't manipulate the DOM and classLists directly like you are doing / would do in vanilla JS.在 React 中,您不会像在 vanilla JS 中那样直接操作 DOM 和 classList。 Instead, it's better to store some state, manipulate the state and then render your view based on the data.相反,最好存储一些状态,操纵状态,然后根据数据呈现您的视图。

So, I would probably do something like this:所以,我可能会做这样的事情:

import React, { useState } from "react"

const Faq = () => {
  const [questions, setQuestions] = useState([
    { id: 1, title: 'How do I make this work?', content: 'Lorem Ipsum', isOpen: true },
    { id: 2, title: 'Help me, Lord?', content: 'You suck at JS', isOpen: false },
    { id: 3, title: 'How does this work. Help?', content: 'Lorem Ipsum.', isOpen: false },
    { id: 4, title: 'Lorem Ipsum?', content: 'Lorem ipsum', isOpen: false },
    { id: 5, title: 'Lorem Ipsum?', content: 'Lorem ipsum', isOpen: false },
  ])

  const toggleFAQ = (questionId) => () => {
    const newQuestions = [
      ...questions,
    ]
    const questionIndex = questions.findIndex(q => q.id === questionId)
    newQuestions[questionIndex].isOpen = !newQuestions[questionIndex].isOpen
    setQuestions(newQuestions)
  }
 
  return (
    <section className="faq">
      <div className="faq__inner">
        <h2 className="faq__title">Frequently Asked Questions</h2>
        <ul className="faq__list">
          {questions.map(q => (
            <li key={q.id} className="faq__item">
              <h3 className="faq__question">
                {q.title}
                <span className="faq__question-btn" onClick={toggleFAQ(q.id)}></span>
              </h3>
              <article className={`faq__answer ${q.isOpen ? 'is-opened' : ''}`}>
                <div className={`faq__answer-inner ${q.isOpen ? 'is-opened' : ''}`}>
                  <p>
                    {q.content}
                  </p>
                </div>
              </article>
            </li>
          ))}
        </ul>
      </div>
    </section>
  )
}

export default Faq

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性“切换”-classList.toggle 反应 - TypeError: Cannot read property 'toggle' of undefined - classList.toggle react 类型错误:无法读取未定义的属性“classList” - TypeError: Cannot read property 'classList' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;classList&#39; - Uncaught TypeError: Cannot read property 'classList' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;classList&#39;-尝试在JS中切换类 - Uncaught TypeError: Cannot read property 'classList' of undefined - Trying to toggle a class in JS 尝试访问脚本时使用 useEffect 在我的 React 项目中获取“TypeError:无法读取 null 的属性 'classList'”? - Getting “TypeError: Cannot read property 'classList' of null” on my React project with useEffect when trying to access a script? 未捕获的类型错误:无法读取未定义的属性(读取“classList”) - Uncaught TypeError: Cannot read properties of undefined (reading 'classList') 未捕获的类型错误:无法读取未定义解决方案的属性“classList/forEach”? - Uncaught TypeError: Cannot read property 'classList/forEach' of undefined solution? TypeError:无法使用 Image-Slider 读取未定义的属性“classList” - TypeError: Cannot read property 'classList' of undefined with Image-Slider 未捕获的类型错误:无法读取 Javascript 中未定义(读取“classList”)的属性“值” - Uncaught TypeError: Cannot read property 'value' of undefined (reading 'classList') in Javascript react/javascript - TypeError:无法读取 null 的属性“classList” - react/javascript - TypeError: Cannot read property 'classList' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM