Skip to content

replace nulls in props from Rails with undefined #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ If you need help upgrading `react-rails`, `webpacker` to `shakapacker`, or JS pa
Changes since last non-beta release.

_Please add entries here for your pull requests that are not yet released._
- Add option to replace `null`s in props with `undefined` via `ReactRailsUJS.setOptions` #1273

### Breaking Changes
- Remove support & testing for Webpacker 3/4.
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,19 @@ use it like so:
ReactUJS.getConstructor = ReactUJS.constructorFromRequireContext(require.context('components', true));
```

### Configure UJS

You can change the behaviours of `ReactRailsUJS` by calling `setOptions(options)` function:

```js
ReactRailsUJS.setOptions({ replaceNull: false });
```

Current acceptable options are the following:

| Key | Value type | Description | Default |
| --- | ---------- | ----------- | ------- |
| `replaceNull` | boolean (`true` / `false`) | Whether to replace all `null`s in the props from Rails (originally `nil` in Rails) with `undefined`. May be helpful when defining the types of the props in TypeScript. See [discussion#1272](https://github.com/reactjs/react-rails/discussions/1272). | `false`|

## Server-Side Rendering

Expand Down
13 changes: 12 additions & 1 deletion react_ujs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var constructorFromRequireContext = require("./src/getConstructor/fromRequireCon
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback")
var constructorFromRequireContextsWithGlobalFallback = require("./src/getConstructor/fromRequireContextsWithGlobalFallback")
const { supportsHydration, reactHydrate, createReactRootLike } = require("./src/renderHelpers")
const { replaceNullWithUndefined, overwriteOption } = require("./src/options")

var ReactRailsUJS = {
// This attribute holds the name of component which should be mounted
Expand All @@ -31,6 +32,15 @@ var ReactRailsUJS = {

components: {},

// Set default values for options.
options: {
replaceNull: false,
},

setOptions: function(newOptions) {
overwriteOption(ReactRailsUJS.options, newOptions, "replaceNull")
},

// helper method for the mount and unmount methods to find the
// `data-react-class` DOM elements
findDOMNodes: function(searchSelector) {
Expand Down Expand Up @@ -106,7 +116,8 @@ var ReactRailsUJS = {
var className = node.getAttribute(ujs.CLASS_NAME_ATTR);
var constructor = ujs.getConstructor(className);
var propsJson = node.getAttribute(ujs.PROPS_ATTR);
var props = propsJson && JSON.parse(propsJson);
var props = propsJson && (ujs.options.replaceNull ? replaceNullWithUndefined(JSON.parse(propsJson))
: JSON.parse(propsJson));
var hydrate = node.getAttribute(ujs.RENDER_ATTR);
var cacheId = node.getAttribute(ujs.CACHE_ID_ATTR);
var turbolinksPermanent = node.hasAttribute(ujs.TURBOLINKS_PERMANENT_ATTR);
Expand Down
18 changes: 18 additions & 0 deletions react_ujs/src/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function replaceNullWithUndefined(obj) {
Object.entries(obj).forEach((entry) => {
const key = entry[0]
const value = entry[1]
if(!!value && typeof value === 'object') {
return replaceNullWithUndefined(value)
}
if (value === null) {
obj[key] = undefined
}
})
return obj
}

export function overwriteOption(ujsOptions, newOptions, key) {
if (!Object.prototype.hasOwnProperty.call(newOptions, key)) return
ujsOptions[key] = newOptions[key]
}