首页 > 解决方案 > 如何使用 react_rails 将子项传递给苗条模板中的组件

问题描述

我正在使用react_rails在 Ruby on Rails 应用程序中的超薄模板内呈现反应组件。有没有办法将子级传递给 React 组件,其中子级在服务器端呈现苗条?

这是我理想中想要的:

<MyComponent>
  <p>my custom content</p>
</MyComponent>

内容来自模板本身,如下所示:

= react_component 'MyComponent' do 
  p my custom content

这可能吗?如果是这样,将孩子传递给 react_component 的最佳方法是什么?

标签: ruby-on-railsreactjsslim-langreact-rails

解决方案


在页面加载时,react_ujs 驱动程序将扫描页面并使用 data-react-class 和 data-react-props 安装组件。

视图助手的签名是: react_component(component_class_name, props={}, html_options={})


推荐阅读