简体   繁体   English

ReactJS:(大)子组件上的 onClick 处理程序不起作用

[英]ReactJS: onClick handler on (grand) child component is not working

I am trying to get a simple page with nested react components using React, Redux.我正在尝试使用 React、Redux 获得一个带有嵌套反应组件的简单页面。 The underlying problem is not due to Redux / connect because if I try to play with code, I am able to dispatch the actions.潜在的问题不是由于 Redux / connect 因为如果我尝试使用代码,我能够调度操作。

My problem is that when I click the image (or even a button), I don't see any event being triggered.我的问题是,当我单击图像(甚至是按钮)时,我没有看到任何事件被触发。

render method of grand child component:孙子组件的渲染方法:

<Fragment>
    <div key={blog.id} className='mb-1 mt-1'>
        <img key={blog.id} src={ path_im } alt={'blog'} width="15" height="15" 
         onClick={this.props.onPrefClick} />
  </div>
</Fragment>

I am new to React, Redux and have tried lot of solutions, that I could find on net, however nothing seems to work in my case and I am stuck on this now.我是 React、Redux 的新手,并尝试了很多解决方案,我可以在网上找到这些解决方案,但是在我的情况下似乎没有任何效果,我现在陷入困境。

The way my components are:我的组件的方式是:

<Dashboard\>
  <BlogList\>

Bloglist (parent component):博客列表(父组件):

  <div>
     { this.props.blogs.map(this.renderBlogItem) }
  </div>
  updatePreference = (blog) => {
      console.log('1 Blog List this.updatePreference: clicked ');
  }

  renderBlogItem = blog => {
      return <BlogItem key={'m0' + blog.id} onBlogClick={() => this.updatePreference(blog)} />;
  }

BlogItem(Immediate Child component) BlogItem(直接子组件)

//this has some JSX static code and below child component
<PreferenceItem key={blog.id} onPrefClick={() => this.props.onBlogClick} />

PreferenceItem (grand child component) PreferenceItem(孙子组件)

//this has some JSX static code and below image with onClick
<div key={blog.id} className='mb-1 mt-1'>
    <img key={blog.id} src={ path_im } alt={'blog'} width="15" height="15" onClick={this.props.onPrefClick}/>

I was hoping that it should be a simple flow of events, but something is not working, I simplified it and changed Click functions in all the three components, changed, bind to fat arrow(=>) and other options, but whenever I click on image, onClick doesn't even getting fired (during playing with my code, when I used bind in grandchild component (with function defined in the same class), it fired the event, but during loading of the page - which is wrong我希望它应该是一个简单的事件流,但是有些东西不起作用,我简化了它并更改了所有三个组件中的 Click 函数,更改了绑定到粗箭头(=>)和其他选项,但是每当我单击在图像上, onClick甚至没有被触发(在玩我的代码期间,当我在孙组件中使用 bind 时(在同一个类中定义了函数),它触发了事件,但是在加载页面期间 - 这是错误的

So something is not right here.所以这里有些不对劲。

Sample working code , which has nested child at one level示例工作代码,它在一层嵌套了子级

I am following mainly official docs from reactjs , react-redux and some reference from the freecodecamp.org :我主要关注 reactjsreact-redux 的官方文档和freecodecamp.org 的一些参考:

One more question还有一个问题

I am now allowed to paste image - Chrome debug tool shows that image element's EventListener's handler is pointing to noop function and FunctionLocation is unknown (why is this noop? This reflects that my bindings are not correct, but I am not able to fix it)我现在可以粘贴图像 - Chrome 调试工具显示图像元素的 EventListener 处理程序指向 noop 函数,而 FunctionLocation 未知(为什么是 noop?这反映了我的绑定不正确,但我无法修复它)

And if I see the grand child's properties, it is pointing to onPrefClick如果我看到孙子的属性,它指向 onPrefClick

Please also direct me so I could debug this to reach the root cause?还请指导我,以便我可以调试它以找到根本原因?

Element is元素是在此处输入图片说明 Component is now referring to onBlogClick组件现在指的是 onBlogClick 在此处输入图片说明

I see one issue here - should be calling the function as well () => this.props.onBlogClick() :我在这里看到一个问题 - 也应该调用该函数() => this.props.onBlogClick()

<PreferenceItem key={blog.id} onPrefClick={() => this.props.onBlogClick} />

I guess this line should be the following:我想这一行应该是以下内容:

<PreferenceItem key={blog.id} onPrefClick={this.props.onBlogClick} />

You were not calling the function, just returning it.您没有调用该函数,只是返回它。

The other issue - maybe it is just a typo in your question but there is a missing { at onClick :另一个问题 - 也许这只是您问题中的一个错字,但在onClick缺少{

<img key={blog.id} src={ path_im } alt={'blog'} width="15" height="15" onClick=this.props.onPrefClick}/>

I hope that helps!我希望这有帮助!

You have syntax errors on this line.您在这一行有语法错误。

<img key={blog.id} src={ path_im } alt={'blog'} width="15" height="15" onClick=this.props.onPrefClick}/>

Above it should be onClick={() => this.props.onPrefClick()} (Also did the modification here).上面应该是onClick={() => this.props.onPrefClick()} (这里也做了修改)。

The Fact事实

When you need call a function in child component which is declared on a parent class you just need the pass the function to child component当您需要调用在父类上声明的子组件中的函数时,您只需要将函数传递给子组件

<ChildComponent function={this.onClickFunction} />

Then you need to invoke the function in child component然后你需要调用子组件中的函数

<img onClick={() => this.props.function()}

In granchild component you should invoke the function.在孙组件中,您应该调用该函数。

<Fragment>
    <div key={blog.id} className='mb-1 mt-1'>
        <img key={blog.id} src={ path_im } alt={'blog'} width="15" height="15" 
         onClick={() => this.props.onPrefClick()} />
  </div>
</Fragment>

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

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