简体   繁体   English

为什么我的 javaScript 代码不能与我的反应组件一起使用?

[英]Why my javaScript code in not working with my react Component?

I cannot add client-side javaScript into my React App component (LeftSection.js).我无法将客户端 javaScript 添加到我的 React App 组件 (LeftSection.js) 中。 Can anyone please tell me what is wrong with my code?谁能告诉我我的代码有什么问题? The one possible answer is that I am adding my javaScript code before JSX (HTML) which does not allow it to work.一个可能的答案是我在 JSX (HTML) 之前添加了我的 javaScript 代码,这不允许它工作。 So, where should I put my script?那么,我应该把我的脚本放在哪里? Also, I am sure that I am also not allowed to put tag in the JSX code.另外,我确信我也不允许在 JSX 代码中添加标签。

Here is the code:这是代码:

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

function LeftSection () {
    var coll = document.getElementsByClassName("collapsible");
    var i;

    for (i = 0; i < coll.length; i++) {
        coll[i].addEventListener("click", function() {
            this.classList.toggle("active");
            var content = this.nextElementSibling;
            if (content.style.display === "block") {
                content.style.display = "none";
            } else {
                content.style.display = "block";
            }
        });
    }
    
    return (
        <div className = 'left-section'>
        <div id = "tool_tree">
            
            <ul>

                <li>
                    <a className = "collapsible">CLM</a>
                    <ul className = "content">  
                        <li>
                            <a className = "collapsible">Commons</a>
                            <ul className = "content"> 
                                <li><a href="#">Workstation</a></li>
                            </ul>
                        </li>
                        
                        <li>
                            <a className = "collapsible">Electrical Characterization</a>
                            <ul className = "content">
                                <li><a href="#">ECL: 4294A Precision Impedance Analyzer</a></li>
                                <li><a href="#">ECL: Cascade 1200 (room temp)</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>

            </ul>
        </div>
        </div>
    );
};

export default LeftSection;

I will really appreciate your help!我将非常感谢您的帮助! Thanks.谢谢。

EDIT (for the subsequent problem I'm facing):编辑(对于我面临的后续问题):

import './LeftSection.css';
import React, { useState } from 'react';


function LeftSection () {
    const [show, setShow] = useState(false);
    
    return (
        <div className = 'left-section'>
        <div id = "tool_tree">
            
            <ul>
                <li>
                    <a className = "collapsible" onClick={() => setShow(!show)}>CLM</a>
                    {show && (
                    <ul className = "content">  
                        <li>
                            <a className = "collapsible"> Commons</a>
                            <ul className = "content"> 
                                <li><a href="#">MCL Commons Imaging Workstation</a></li>
                            </ul>
                        </li>
                        
                        <li>
                            <a className = "collapsible">Electrical Characterization</a>
                            <ul className = "content">
                                <li><a href="#">ECL: 4294A Precision Impedance Analyzer</a></li>
                                <li><a href="#">ECL: Cascade 1200 (room temp)</a></li>
                                <li><a href="#">ECL: Ecopia HMS-3000 Hall Measurement</a></li>

                            </ul>
                        </li>
                        <li>
                            <a className = "collapsible">Electron Microscopy</a>
                            <ul className = "content">
                                <li><a href="#">EPMA: CAMECA SXFive</a></li>
                                <li><a href="#">ESEM: Q250</a></li>
                                <li><a href="#">EXpressLO LLC Station $0</a></li>
                            </ul>
                        </li>
                    </ul>
                    )}
                </li>
                






                <li>
                    <a className = "collapsible" onClick={() => setShow(!show)}> 2DC </a>
                    {show && (
                    <ul className = "content">  
                        <li>
                            <a className = "collapsible">Commons</a>
                            <ul className = "content"> 
                                <li><a href="#">MCL Commons Imaging Workstation</a></li>
                            </ul>
                        </li>
                        <li>
                             <a className = "collapsible">Electrical Characterization</a> 
                            <ul className = "content">
                                <li><a href="#">ECL: Poling</a></li>
                                <li><a href="#">ECL: Seebeck</a></li>
                                <li><a href="#">ECL: Sonelastic</a></li>
                                
                            </ul>
                        </li>
                    </ul>
                    )}
                </li>






                <li>
                    <a className = "collapsible" onClick={() => setShow(!show)}> Nanofab </a>                        
                    {show && (
                    <ul className = "content">  
                        <li>
                        <a className = "collapsible">Commons</a>
                            <ul className = "content"> 
                                <li><a href="#">MCL Commons Imaging Workstation</a></li>
                            </ul>
                        </li>
                        <li>
                        <a className = "collapsible">Electrical Characterization</a> 
                            <ul className = "content">
                                <li><a href="#">ECL: Poling</a></li>
                                <li><a href="#">ECL: Seebeck</a></li>
                            </ul>
                        </li>
                    </ul>
                    )}
                </li>
            </ul>
        </div>
    </div>

    );
    
};


export default LeftSection;

I added the code (without the styling) in an online editor and it's working.我在在线编辑器中添加了代码(没有样式)并且它正在工作。 Can you be more precise with your question?你能更准确地回答你的问题吗? Also in React you shouldn't grab elements from the dom like you do in Javascript because it cancel all advantages that React brings with virtual DOM.同样在 React 中,您不应该像在 Javascript 中那样从 dom 中获取元素,因为它取消了 React 为虚拟 DOM 带来的所有优势。 Please check React documentation about passing event handlers to components: https://reactjs.org/docs/faq-functions.html#how-do-i-pass-an-event-handler-like-onclick-to-a-component请查看有关将事件处理程序传递给组件的 React 文档: https ://reactjs.org/docs/faq-functions.html#how-do-i-pass-an-event-handler-like-onclick-to-a-component

Also here a simple hook example for adding an onClick event: https://reactjs.org/docs/hooks-state.html这里还有一个简单的钩子示例,用于添加 onClick 事件: https ://reactjs.org/docs/hooks-state.html

You should add onClick event to corresponding element from jsx and then handle it in a function that you declare in the same component.您应该将 onClick 事件添加到 jsx 中的相应元素,然后在您在同一组件中声明的函数中处理它。

 import React, { useState } from "react"; function LeftSection() { const [show, setShow] = useState(false); return ( <div className="left-section"> <ul> <li> <a>CLM</a> <ul> <li> <a onClick={() => setShow(!show)}>Commons</a> {show && ( <ul className="content"> <li> <a href="#">Workstation</a> </li> </ul> )} </li> </ul> </li> </ul> </div> ); } export default LeftSection;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM