Skip to content

Request: Provide a Helper for Camelizing Props #530

@kevinthompson

Description

@kevinthompson

Help us help you! Please choose one:

  • My app crashes with react-rails, so I've included the stack trace and the exact steps which make it crash.
  • My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.
  • I have another issue to discuss.

Issue

Props are currently camelized through a private instance method on React::Rails::ComponentMount:
https://github.com/reactjs/react-rails/blob/master/lib/react/rails/component_mount.rb#L51-L64

While this works for the initial rendering on a component, if the component needs to be re-rendered via update.js.erb, for example, the props have to manually mapped, keys have to be translated, or we need to tap into the existing private method:

// update.js.erb

ReactDOM.render(
  React.createElement(ExampleComponent, <%= raw({ 
    someLongKeyName: "value" 
  }.to_json) %>,
  document.getElementById("example_component_id")
)

Workaround

# application_helper.rb

def camelize_props(props = {})
  React::Rails::ComponentMount.new.send(:camelize_props_key, props)
end
// update.js.erb

ReactDOM.render(
  React.createElement(ExampleComponent, <%= raw(camelize_props({ 
    some_long_key_name: "value" 
  }.to_json) %>,
  document.getElementById("example_component_id")
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions