ruby-on-railsreactjshamlreact-rails

Using react_component() to add react components in haml files


While working with the react-rails library, we can use the react_component() function in an .erb file to add a react component.

How can we use the same function in a .haml file? Or do we have another way of adding a react component in a haml file?


Solution

  • If webpacker is setup in your RoR project, then you can simply use the react_component function from react-rails in the same way we would add ruby code to the haml file.

    Ex:

    %li
      = react_component("ReactComponent")
    

    This should work correctly. Webpacker will try to locate the React component in app/javascript/components by default.