On the code below I expected that (console logged) object props
only contains member: className
but it contains members: { className, id, ticket, request }
. However, if I remove param ...others
from non used function: NullComp
, then the only member of object props
is: className
, which is what I was expecting initially.
You can try it by yourself by running the code below:
with: ...others
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Test</title>
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function GoalComp({
id,
ticket,
request,
...props
}) {
console.log(props);
return <div>Independent Retriever</div>;
}
</script>
<script type="text/babel">
function NullComp({ timeRanges, ...others }) {
return null;
}
</script>
<script type="text/babel">
const { useState, useEffect } = React;
const App = () => {
return (
<GoalComp
className="mt-10"
id="1"
ticket="IT-ABCD"
request="Peace and Love"
/>
);
};
ReactDOM.render(<App />, document.querySelector("#root"));
</script>
</body>
</html>
without: ...others
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Test</title>
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function GoalComp({
id,
ticket,
request,
...props
}) {
console.log(props);
return <div>Independent Retriever</div>;
}
</script>
<script type="text/babel">
function NullComp({ timeRanges }) {
return null;
}
</script>
<script type="text/babel">
const { useState, useEffect } = React;
const App = () => {
return (
<GoalComp
className="mt-10"
id="1"
ticket="IT-ABCD"
request="Peace and Love"
/>
);
};
ReactDOM.render(<App />, document.querySelector("#root"));
</script>
</body>
</html>
What I’m I missing here?
2
Answers
The behavior you’re observing is actually expected and is not influenced by the presence or absence of the
...others
in theNullComp
function. The key thing to note is that theNullComp
function isn’t being used or invoked anywhere in the provided code, so it has no effect on the output of theGoalComp
function.Let’s break down what’s happening:
In the
GoalComp
function, you’re using destructuring to extract theid
,ticket
, andrequest
properties from the props. The rest of the properties (if any) are collected into theprops
object using the spread operator (...props
).When you render the
GoalComp
component inside theApp
component, you’re passing the following props:className="mt-10"
id="1"
ticket="IT-ABCD"
request="Peace and Love"
Given this, when these props are passed to
GoalComp
, theid
,ticket
, andrequest
properties are extracted, leaving theclassName
property to be collected into theprops
object. This is why, when youconsole.log(props)
, you see an object with a single property:{ className: "mt-10" }
.The presence or absence of the
...others
in theNullComp
function is irrelevant in this context. SinceNullComp
isn’t being used, whether it has...others
or not doesn’t affect the output of theGoalComp
function.In summary, the code is working as expected. The
NullComp
function isn’t influencing the output of theGoalComp
function in any way.Hope this helps!
Using babel standalone is really not meant for production.
But out of curiosity I thought it would be fun to see why this breaks.
If you compile
GoalComp
in babel, for a target that doesn’t support spread syntax, you will notice it creates a global ->On the other hand if you compiled both
GoalComp
&NullComp
with the spread operators together you will get ->But if you do them as separate scripts/compiles.
You would end up with.
To get around this you could place the functions on a global, but you will also need to set the scripts
data-type="module"
to keep those_excluded
scoped locally.eg.