简体   繁体   English

防止 onClick 在负载时被触发。 我在 React 项目中使用功能组件

[英]Prevent onClick getting fired on load. I'm using the functional component on a React project

This is the Parent Component which passes a reference to the cart component这是传递对购物车组件的引用的父组件

  function Header() {
    
      const cartRef = useRef();
    
      const handleClick = () =>{
        cartRef.current.style.boxShadow = "0 0 0 100vw rgb(0, 0, 0, 0.9)"
      }
    
        return (
                  <>
                    <Cart cRef={cartRef} onClick={()=>{handleClick()}}/>
                  </>
                );
      }
      
      export default Header;

The Child Component assigns the reference to the div shown below子组件将引用分配给如下所示的 div

    function Cart(props) {
    
        return (<>
                  <div ref={props.cRef}>
                     ...
                  </div>
            </>
        );
    }

export default  Cart;

Does passing references like this cause such issues?像这样传递引用会导致此类问题吗?

Try it like this: <Cart cRef={cartRef} onClick={handleClick}/>试试这样: <Cart cRef={cartRef} onClick={handleClick}/>

I think the handleClick fires because it's passed in a wrong way.我认为handleClick触发是因为它以错误的方式传递。

The handleClick() isn't actually getting fired. handleClick() 实际上并没有被解雇。 Whats happening is that the Cart component that has been imported to the Header component will be visible like any component does when placed in the return statement.发生的情况是,已导入到 Header 组件的 Cart 组件将像放置在 return 语句中的任何组件一样可见。

function Header() {
    
      const cartRef = useRef();
    
      const handleClick = () =>{
        cartRef.current.style.visibility = "visible";
        cartRef.current.style.boxShadow = "0 0 0 100vw rgb(0, 0, 0, 0.9)"
      }
    
        return (
                  <>
                    <Cart cRef={cartRef} onClick={()=>{handleClick()}}/>
                  </>
                );
      }
      
      export default Header;

We can set the Cart component's Visibility to 'hidden' and then on the handleClick() event handler, we set it back to 'visible' again.我们可以将 Cart 组件的Visibility设置为“隐藏” ,然后在 handleClick() 事件处理程序上,我们再次将其设置回“可见”

暂无
暂无

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

相关问题 onClick 使用 firestore 和 React、redux 在组件挂载时立即触发 - onClick gets fired immediately on component mount using firestore and React, redux onClick 在 React 功能组件中不工作 - onClick not working in React functional component <a>使用 onClick 和 TypeScript</a>响应功能<a>组件</a> - React functional <a> component with onClick and TypeScript 我在我的组件中使用 react bootstrap modal,但它没有通过另一个组件中的 onClick 事件调用打开 - I'm using react bootstrap modal in my component but it does not open by onClick event call in another component 在功能组件中没有得到我期望的变量的原因是什么? - What's the reason for not getting the variable I'm expecting in the functional component? 我已经在使用功能组件,但我不断收到错误消息:只能在功能组件的主体内调用钩子 - I'm already using a functional component and I keep getting error : Hooks can only be called inside the body of a function component 将功能组件与映射的子功能组件与 onClick function 反应 - React functional component with mapped Child functional component with onClick function 我收到 TypeError:尝试在 React 的功能组件中使用道具时无法设置未定义的属性“道具”? - I'm getting TypeError: Cannot set property 'props' of undefined while trying to use props inside functional component in React? 按钮 onClick 在反应功能组件中不起作用 - Button onClick is not working inside a react functional component 如何在 React 功能组件中的 onClick 上传递参数 - How to pass parameter on onClick in React Functional Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM