Skip to content

Commit 6d1bace

Browse files
committed
Add functionality to replace null in props with undefined
1 parent a644607 commit 6d1bace

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

react_ujs/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var constructorFromRequireContext = require("./src/getConstructor/fromRequireCon
88
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback")
99
var constructorFromRequireContextsWithGlobalFallback = require("./src/getConstructor/fromRequireContextsWithGlobalFallback")
1010
const { supportsHydration, reactHydrate, createReactRootLike } = require("./src/renderHelpers")
11+
const { replaceNullWithUndefined } = require("./src/options")
1112

1213
var ReactRailsUJS = {
1314
// This attribute holds the name of component which should be mounted
@@ -31,6 +32,11 @@ var ReactRailsUJS = {
3132

3233
components: {},
3334

35+
// Set default values for options.
36+
options: {
37+
replaceNull: false,
38+
},
39+
3440
// helper method for the mount and unmount methods to find the
3541
// `data-react-class` DOM elements
3642
findDOMNodes: function(searchSelector) {
@@ -106,7 +112,8 @@ var ReactRailsUJS = {
106112
var className = node.getAttribute(ujs.CLASS_NAME_ATTR);
107113
var constructor = ujs.getConstructor(className);
108114
var propsJson = node.getAttribute(ujs.PROPS_ATTR);
109-
var props = propsJson && JSON.parse(propsJson);
115+
var props = propsJson && (ujs.options.replaceNull ? replaceNullWithUndefined(JSON.parse(propsJson))
116+
: JSON.parse(propsJson));
110117
var hydrate = node.getAttribute(ujs.RENDER_ATTR);
111118
var cacheId = node.getAttribute(ujs.CACHE_ID_ATTR);
112119
var turbolinksPermanent = node.hasAttribute(ujs.TURBOLINKS_PERMANENT_ATTR);

react_ujs/src/options.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function replaceNullWithUndefined(obj) {
2+
Object.entries(obj).forEach((entry) => {
3+
const key = entry[0]
4+
const value = entry[1]
5+
if(!!value && typeof value === 'object') {
6+
return replaceNullWithUndefined(value)
7+
}
8+
if (value === null) {
9+
obj[key] = undefined
10+
}
11+
})
12+
return obj
13+
}

0 commit comments

Comments
 (0)