使用 react_component() 在haml文件中添加react组件

问题描述

在使用 react-rails 库时,我们可以使用 .erb 文件中的 react_component() 函数添加反应组件。

我们如何在 .haml 文件中使用相同的函数?或者我们还有其他方法可以在haml文件添加react组件吗?

解决方法

如果在您的 RoR 项目中设置了 webpacker,那么您可以简单地使用 react_component 中的 react-rails 函数,就像我们将 ruby​​ 代码添加到 haml 文件一样。

例如:

%li
  = react_component("ReactComponent")

这应该可以正常工作。默认情况下,Webpacker 会尝试在 app/javascript/components 中定位 React 组件。