微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在reactJS函数setPanel?

如何解决在reactJS函数setPanel?

所有人。

我有一个使用香草javascript的手风琴,我想用React Hooks来实现。我试图添加到jsx,但是我的方法会产生错误

我不知道rsx如何适应香草JavaScript函数setPanel。

 func SendFiletoClient(w http.ResponseWriter,r *http.Request,file string){

    downloadBytes,err := IoUtil.ReadFile(file)
    fmt.Println("file to be sent ",file)
    if err != nil {
        fmt.Printf("unable to download the file: %v",err)
    }

    mime := http.DetectContentType(downloadBytes)
    fileSize := len(string(downloadBytes))
   fmt.Println("mime is ",mime," filesize ",fileSize)
    //Generate the server headers
    w.Header().Set("Content-Type","octet-stream")
    w.Header().Set("Content-disposition","attachment; filename="+file+"")
    w.Header().Set("Expires","0")
    w.Header().Set("Content-transfer-encoding","binary")
    w.Header().Set("Content-Length",strconv.Itoa(fileSize))
    w.Header().Set("Content-Control","private,no-transform,no-store,must-revalidate")


    
    //// force it down the client's.....
    http.ServeContent(w,r,file,time.Now(),bytes.NewReader(downloadBytes))



}

这是我的组件代码

TypeError: panel.setPanel is not a function
HTMLButtonElement.<anonymous>
Accordion.js:12
   9 | acc[i].addEventListener("click",function() {
  10 |  this.classList.toggle("active");
  11 |  let panel = this.nextElementSibling;
> 12 |  panel.setPanel( this.nextElementSibling);
     | ^  13 |  if (panel.style.display === "block") {
  14 |      panel.style.display = "none";
  15 |  } else {

原始的原始JavaScript代码

import React,{ useEffect} from "react";

export default function Acordeon()
{
    const setStyle= () => {
        let acc = document.getElementsByClassName("accordion");
        for (let i = 0; i < acc.length; i++)
        {
            acc[i].addEventListener("click",function() {
                this.classList.toggle("active");
                let panel = this.nextElementSibling;
                panel.setPanel( this.nextElementSibling);
                if (panel.style.display === "block") {
                    panel.style.display = "none";
                } else {
                    panel.style.display = "block";
                }
            });
        }
    }

    useEffect(()=>{
        setStyle()
    });

    return({/*HTML Code with some css classes*/}
    )
}

在我的HTML代码中,有一个带有onClick事件的按钮以显示手风琴的内容,再次单击时将其隐藏

解决方法

使用此代码可以解决问题

useEffect(() => {
    let acc = document.getElementsByClassName("accordion");
    for (let i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click",function()
        {
            this.classList.toggle("active");

            var panel = this.nextElementSibling;
            if (panel.style.display === "block")
            {
                panel.style.display = "none";
            }

            else
            {
                panel.style.display = "block";
            }
        });
    }
    
},[])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。