Skip to content
Advertisement

React functional component with mapped Child functional component with onClick function

Hi I have mapped some json data named “projectsData” and I am trying to “bind” an onClick event with a setState hook. The mapping works except for the “onClick” does not work when clicking the grid item. In my case I want to update filterproject value with the project.id value from that target.

Right now when I click an item it does nothing.

How do I successfully map a function to “onClick” while using functional components?

Below is the parent Component

JavaScript

And here is the Child Component – “Project”

JavaScript

below is a screen grab of Console showing one of the mapped projects and it’s onClick parameters. I can see it but when I click nothing happens. Any help would be great! console output of {projects}

Advertisement

Answer

You pass click handler to a prop called onClick when setting initial state

JavaScript

but access it as projectClick in the component

JavaScript

Fix by accessing the correct prop

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement