Module: ReactOnRails::Controller

Defined in:
lib/react_on_rails/controller.rb

Instance Method Summary collapse

Instance Method Details

#redux_store(store_name, props: {}, **rest) ⇒ Object

Separate initialization of store from react_component allows multiple react_component calls to use the same Redux store.

store_name: name of the store, corresponding to your call to ReactOnRails.registerStores in your

JavaScript code.

props: Named parameter props which is a Ruby Hash or JSON string which contains the properties

to pass to the redux store.

Be sure to include view helper ‘redux_store_hydration_data` at the end of your layout or view or else there will be no client side hydration of your stores.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/react_on_rails/controller.rb', line 15

def redux_store(store_name, props: {}, **rest)
  immediate_hydration_present = rest.key?(:immediate_hydration)
  unknown_keys = rest.keys - [:immediate_hydration]
  if unknown_keys.any?
    plural = unknown_keys.one? ? "" : "s"
    unknown_options = unknown_keys.map { |key| ":#{key}" }.join(", ")
    raise ArgumentError, "unknown keyword#{plural}: #{unknown_options}"
  end

  ReactOnRails::Helper.warn_removed_immediate_hydration_option("redux_store") if immediate_hydration_present

  redux_store_data = { store_name: store_name,
                       props: props }
  @registered_stores_defer_render ||= []
  @registered_stores_defer_render << redux_store_data
end