简体   繁体   English

Reactjs app div onclick 在 IE 11 中不工作没有 function 被调用

[英]Reactjs app div onclick not working in IE 11 no function getting called

I am working a project with reactJS as the main framework and using latest react hooks.我正在使用 reactJS 作为主要框架并使用最新的反应挂钩的项目。 I am having a const function need to execute on a div item (child-component) on the left side, which consists of simple head and some padding kind of box.我有一个 const function 需要在左侧的 div 项(子组件)上执行,它由简单的头部和一些填充类型的框组成。

here is my code Function will get called in parent component这是我的代码 Function 将在父组件中调用

    import React from 'react'
    import PropTypes from 'prop-types'
    import classnames from 'classnames'
    import withStyles from 'react-jss'
    import * as R from 'ramda'
    import { connect } from 'react-redux'
    import CardItem from './card-item'

    const ParentComponent = ({
      classes
    }) => {
    const arrayData = [{name: "Jose", id: "1"}, {name: "Job", id: "2"}];
    const onSelect = (data) => () => {
        console.log(data)
    }

    return (<div> 
    {Array.map(item => 
     <CardItem 
      item={item}
      onclickFun={onSelect(item)}
     />,(arrayData))}
     </div>
     )
    }

    ParentComponent.propTypes = {
      classes: PropTypes.object
    }

    const styles = theme => ({
      card: {
        boxSizing: 'border-box',
        minHeight: '150px',
        width: '100%',
        borderStyle: 'solid',
        borderWidth: '1px',
      }

    })

    export default (withStyles(styles)(ParentComponent))

The card Component code:卡片组件代码:

            import React from 'react'
            import PropTypes from 'prop-types'
            import classnames from 'classnames'
            import withStyles from 'react-jss'
            import * as R from 'ramda'
            import { connect } from 'react-redux'

            const CardItem = ({
              classes,
              item,
              onclickFun
            }) => {
              return (
                <div className=[classes.card} onClick={onclickFun}>
                  <div className={classes.topContainer}>
                    {item.name}
                  </div>
                </div>
              )
            }

            CardItem.propTypes = {
              classes: PropTypes.object,
              item: PropTypes.object,
              onClick: PropTypes.func
            }

            const styles = theme => ({
              card: {
                boxSizing: 'border-box',
                minHeight: '150px',
                width: '100%',
                borderStyle: 'solid',
                borderWidth: '1px',
              }

            })

            export default (withStyles(styles)(CardItem))

Please help请帮忙

You have an extra => () in your onSelect function您的 onSelect function 中有一个额外的=> ()

Replace代替

const onSelect = (data) => () => {
    console.log(data)
}

With

const onSelect = (data) => {
    console.log(data)
}

Also when adding as prop to the cardItem you're calling the function as apposed to passing the actual function like so此外,当您将作为道具添加到 cardItem 时,您将调用 function 与传递实际的 function 一样

    onclickFun={() => {onSelect(item)}}

Replace代替

 <CardItem 
   item={item}
   onclickFun={onSelect(item)}
 />

With

 <CardItem 
    item={item}
    onclickFun={() => {onSelect(item)}}
 />

One more thing Array.map should be called on the array directly like so另一件事 Array.map 应该像这样直接在数组上调用

{arrayData.map(item => 
 <CardItem 
  item={item}
  onclickFun={onSelect(item)}
 />)}

I created a working sandbox (I removed all styles to simplify it) Code sandbox我创建了一个工作沙箱(我删除了所有 styles 以简化它)代码沙箱

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

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