Skip to content

Commit e77835e

Browse files
committed
Add ruby logic to replace null with undefiend in props json
1 parent feeacb4 commit e77835e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/react/rails/component_mount.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def generate_html_options(name, options, props, prerender_options)
5656
unless prerender_options == :static
5757
html_options[:data].tap do |data|
5858
data[:react_class] = name
59-
data[:react_props] = (props.is_a?(String) ? props : props.to_json)
59+
data[:react_props] = props_to_json(props)
6060
data[:hydrate] = "t" if prerender_options
6161

6262
num_components = @cache_ids.count { |c| c.start_with? name }
@@ -67,6 +67,15 @@ def generate_html_options(name, options, props, prerender_options)
6767
html_options
6868
end
6969

70+
def props_to_json(props)
71+
return props if props.is_a?(String)
72+
return props.to_json unless Dummy::Application.config.react.null_to_undefined_props
73+
74+
# This regex matches key:value with null values while ensuing no string with similar
75+
# pattern gets matched. It doesn't include null values in arrays.
76+
props.to_json.gsub(/([^\\]":)null([,}\]])/, '\1undefined\2')
77+
end
78+
7079
def rendered_tag(html_options, &block)
7180
html_tag = html_options[:tag] || :div
7281

lib/react/rails/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Railtie < ::Rails::Railtie
1212
config.react.jsx_transformer_class = nil # defaults to BabelTransformer
1313
config.react.camelize_props = false # pass in an underscored hash but get a camelized hash
1414
config.react.sprockets_strategy = nil # how to attach JSX to the asset pipeline (or `false` for none)
15+
config.react.null_to_undefined_props = false # Set to true to convert null values in props into undefined
1516

1617
# Server rendering:
1718
config.react.server_renderer_pool_size = 1 # increase if you're on JRuby

test/react/rails/component_mount_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,23 @@ def self.react_rails_prerenderer
128128

129129
assert_equal %(<div>rendered Foo with {&quot;ok&quot;:true}</div>), rendered_component
130130
end
131+
132+
test "#react_component sets null props to undefined when null_to_undefined_props set to true" do
133+
app.config.react.null_to_undefined_props = true
134+
135+
@helper.setup(DummyController)
136+
rendered_component = @helper.react_component("Foo", { bar: nil, content: 'bar":null,' })
137+
138+
assert_includes rendered_component, '&quot;bar&quot;:undefined,&quot;content&quot;:&quot;bar\\&quot;:null,&quot;'
139+
end
140+
141+
test "#react_component passes null props as null when null_to_undefined_props set to false" do
142+
app.config.react.null_to_undefined_props = false
143+
144+
@helper.setup(DummyController)
145+
rendered_component = @helper.react_component("Foo", { bar: nil, content: 'bar":null,' })
146+
147+
assert_includes rendered_component, "&quot;bar&quot;:null,&quot;content&quot;:&quot;bar\\&quot;:null,&quot;"
148+
end
131149
end
132150
end

0 commit comments

Comments
 (0)