Option to replace null values in data-react-props attribute with undefined #1272
Replies: 3 comments 4 replies
-
It makes sense. @justin808 if you agree with this change, should add it to v3? |
Beta Was this translation helpful? Give feedback.
-
@MaySoMusician why does this need to go into the library? It shouldn't. I think your example makes sense, but the right place to put the code is after your JS code receives the props. Either convert the props when received in your own library method, which you provided in this PR. Or in JavaScript, there are a few ways to check if a variable is undefined or null. The From chatgpt: Using the typeof operator:You can use the typeof operator to check if a variable is undefined. If the variable is null, typeof will also return "object". Here's an example: let x;
if (typeof x === 'undefined' || x === null) {
// x is undefined or null
} Using the == or === operator:You can also use the == or === operator to check if a variable is undefined or null. The == operator performs type coercion, so it will return true if the variable is either undefined or null. The === operator checks for strict equality, so it will only return true if the variable is exactly undefined or null. Here's an example: let x;
if (x == null) {
// x is undefined or null
}
if (x === undefined || x === null) {
// x is exactly undefined or null
} Using optional chaining:If you are using a recent version of JavaScript that supports optional chaining, you can use the ?. operator to check if a variable is undefined or null. Here's an example: const obj = {
prop: null
};
if (obj.prop?.foo === undefined) {
// obj.prop.foo is undefined or null
} Note that in the last example, if obj.prop is null, obj.prop?.foo will evaluate to undefined, and the condition will be true. If obj.prop is undefined, obj.prop?.foo will also evaluate to undefined, and the condition will be true. If obj.prop is an object with a foo property, obj.prop?.foo will evaluate to the value of the foo property, and the condition will be false. |
Beta Was this translation helpful? Give feedback.
-
Closing discussion since it's been agreed to implement this detail. See #1273 |
Beta Was this translation helpful? Give feedback.
-
I'm currently working on a Rails+React project, and planning to eliminate
null
s from type definitions in the frontend and to replace them withundefined
(or just use optional props likeprop?: Foo
) as possible, since I can't be bothered about nervous distinction betweennull
andundefined
when defining the types.react-rails passes the values from Rails to React via
data-react-props
HTML attribute with JSON, in the specification of whichundefined
is not defined (no pun intended). Due to this, in the part of converting them to JSON, we have no choice than converting Ruby'snil
s to JSON'snull
s.But on the other hand, we could convert JSON's
null
s either to JS'snull
(current implementation) or to itsundefined
. I want an option to select one of that ways.Beta Was this translation helpful? Give feedback.
All reactions