JavaScript for WordPress Forums Gutenberg Development Creating reusable components

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #40939
    mikemcalister
    Participant

    Hey everyone,

    I’m working on making some of my components more usable and trying to reduce duplicate code. In this sample gist, you can see I’m using <SocialMediaIcons> to output a chunk of social links. This works great in the edit function, but breaks the block if I try to put it in the save function. Any thoughts on how to make this component available in the save function as well? Thanks in advance for any thoughts!

    #41006
    Zac Gordon
    Keymaster

    Unfortunately there are some issues with getting JS driven blocks to work on the frontend, as crazy as that sounds. This is something they are working on. This may be related, but may not be.

    What specifically breaks in this example?

    #41010
    mikemcalister
    Participant

    When I try to reference the SocialMediaIcons component in the save function, I get an undefined error. “ReferenceError: SocialMediaIcons is not defined.” That component is defined in the edit function and works as expected in that sense.

    #41037
    Zac Gordon
    Keymaster

    Aw, missed this at first glance. Simple solution 🙂

    You’re defining the component inside your edit function.

    It’s best to pull it out into its own file or at least up outside of the registerBlockType function 🙂

    Let me know if that doesn’t work!

    #41146
    mikemcalister
    Participant

    That actually worked great! I extracted that out to its own file and everything is smooth sailing. Thanks, Zac!

    #41151
    Zac Gordon
    Keymaster

    Win for component architecture 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.