Skip to content
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

JSX unnamed export default is subtly broken #10077

Open
Bludator opened this issue Jan 24, 2025 · 0 comments
Open

JSX unnamed export default is subtly broken #10077

Bludator opened this issue Jan 24, 2025 · 0 comments

Comments

@Bludator
Copy link

🐛 bug report

If I write component with default export and without name like: export default function(){...} the component is getting unmounted/mounted instead of just rerendered. It is different to the default export with a name like export default function Foo(){...}. The name is not important. It happens only in watch and serve mode.

🎛 Configuration (.babelrc, package.json, cli command)

{
    "parcel": "^2.13.3",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
}

🤔 Expected Behavior

It should be the same like with name.

In the following code sample I expect to see only Test mounting once and then repeating of Rendering...

😯 Current Behavior

In watch and serve it differs from the named function.

In the sample code I am getting repeatedly:
Rendering...
Test unmounting
Test mounting

💁 Possible Solution

🔦 Context

💻 Code Sample

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div id="root"></div>

    <script type="module" src="index.jsx"></script>
  </body>
</html>

index.jsx

import React from 'react';
import ReactDOM from 'react-dom/client';

import { App } from './app.jsx'

ReactDOM.createRoot( 
  document.querySelector('#root')
).render(<App />)

app.jsx

import { useState, useEffect } from 'react';
import Test from "./test.jsx"

export function App() {
	const [foo, setFoo] = useState();

	console.debug("Rendering...")

	// every second change state randomly
	useEffect(function () {
		const interval = setInterval(() => Promise.resolve().then(_=>setFoo(Math.random())), 1000)
		return () => clearInterval(interval)
	}, [])
	return (<Test />)
}

test.jsx

import { useEffect } from 'react';

export default function () {
	// This just prints when it is mounting/unmounting
	useEffect(() => {
		console.log("Test mounting");
		return () => console.log("Test unmounting")
	}, []);
	return <span style={{ color: "red" }}>Lorem ipsum</span>
}

🌍 Your Environment

Software Version(s)
Node v23.6.0
npm/Yarn npm - 11.0.0
Operating System win11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant