简体   繁体   English

反应 onClick 不传播事件

[英]React onClick not propagating event

I created a new reactjs project, somehow onClick={() => console.log('clicked...')} does not propagate an event, even though the component is rendered on the DOM.我创建了一个新的 reactjs 项目,不知何故onClick={() => console.log('clicked...')}不会传播事件,即使组件在 DOM 上呈现。

However, the same code propagates an event when I tried it on a JSFiddle.但是,当我在 JSFiddle 上尝试时,相同的代码会传播一个事件。

Help please.请帮忙。

EDIT编辑

import React from 'react'


const Key = props => {
    return (<button
     style={style} 
     className={'calc-key ' + props.element.name} 
    //  onClick={props.handleButtonClick}
     onClick={() => console.log('Clicked...')}
     >
        {props.element.value}
    </button>)
}

const style = {
    width: '33%',
    textAlign: 'center',
    padding: '1em',
    fontWeight: '800',
    cursor: 'pointer',
    zIndex: '100'
    
}

export default Key;
import React from 'react'

const Key = props => {
  return (<button
    style={style}
    className={'calc-key ' + props?.element?.name}
    onClick={() => console.log('Clicked...')}
  >
    {props?.element?.value}
  </button>)
}

const style = {
  width: '33%',
  textAlign: 'center',
  padding: '1em',
  fontWeight: '800',
  cursor: 'pointer',
  zIndex: '100'

}

export default Key;

i tried your code, its useful.我试过你的代码,它很有用。 在此处输入图像描述

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

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