在應用程式中使用反應軌

React.js 構建

你可以通過新增配置來選擇在每個環境中構建(開發,生產,有或沒有附加元件)的 React.js。以下是預設值:

# config/environments/development.rb
MyApp::Application.configure do
  config.react.variant = :development
end

# config/environments/production.rb
MyApp::Application.configure do
  config.react.variant = :production
end

要包含載入項,請使用此配置:

MyApp::Application.configure do
  config.react.addons = true # defaults to false
end

重新啟動 Rails 伺服器後,// = require react 將提供由配置指定的 React.js 的構建。

react-rails 為 React.js 的版本和構建提供了一些其他選項。有關使用 react-source gem 或刪除自己的 React.js 副本的詳細資訊,請參閱 VERSIONS.md。

JSX

安裝 react-rails 後,重新啟動伺服器。現在,.js.jsx 檔案將在資產管道中進行轉換。

BabelTransformer 選項

你可以使用 babel 的變換器和自定義外掛,並將選項傳遞給 babel 轉換器,新增以下配置:

config.react.jsx_transform_options = {
  blacklist: ['spec.functionName', 'validation.react', 'strict'], # default options
  optional: ["transformerName"],  # pass extra babel options
  whitelist: ["useStrict"] # even more options[enter link description here][1]
}

在引擎蓋下,react-rails 使用 ruby-babel-transpiler 進行轉換。