Skip to content
Advertisement

How can I call the function of a parent component inside of a child component?

First off, I’m newer to react so please feel free to critique any sort of architectural problems I have, I feel like there’s a better way to write this but I’ve been struggling for an hour trying to get it to work.

Parent Element (TileGrid):

JavaScript

Child Element (Tile):

JavaScript

So there’s a function inside of TileGrid called updateTileData that is going to take some data and use it to update the state of TileGrid. The Tile component exists as a child within the TileGrid component. I’ve tried all sorts of stuff on here but this seems like a simple task to do, is there a better way to write this functionality? I’m not tied down to anything in the code, please tell me if there’s a better way to do this. I’m still learning this framework and this issue has me hung up. Thanks, Archer.

Advertisement

Answer

pass the function from parent to child as a prop example :

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