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

Since 5.7.2: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found. #60603

Closed
RobertSandiford opened this issue Nov 26, 2024 · 3 comments Β· Fixed by #60687
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@RobertSandiford
Copy link

RobertSandiford commented Nov 26, 2024

πŸ”Ž Search Terms

Search/Keywords: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.

πŸ•— Version & Regression Information

Since 5.7.2. No 5.7.0 or 5.7.1 available. OK in 5.6.3

⏯ Playground Link

Best effort Playground

πŸ’» Code

    <>
        <h1>Logged Out</h1>
    </>

πŸ™ Actual behavior

Since TS 5.7.2 JSX Fragments are no longer accepted.

Compile error: error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.

πŸ™‚ Expected behavior

JSX Fragments should continue to work

Additional information about the issue

I have a custom JSX setup, I'm not using React. babel-preset-react is used to transform JSX. This grabs Fragment from the JSX runtime provided and passes this as the node type when creating the element via the JSX runtime. It has been working since TS 4 until now.

Compiled output:

import { jsx as _jsx, Fragment as _Fragment } from "tungsten/jsxRuntime.js";
...
    _jsx(_Fragment, {
        children: _jsx("h1", {
            children: "Logged Out"
        })
    })

The error message about React is odd, JSX and React are independent, why would React anything be needed to use JSX fragments? babel-preset-react with runtime: "automatic" loads jsx functions and the Fragment constant from a specified JSX runtime and does not rely on anything called React.

@iisaduan
Copy link
Member

What JSX setting are you currently using in your tsconfig? The "requires React to be in scope" sounds like it could be hitting a default error message, so knowing your jsx setting would help me diagnose the issue

Since you are using a custom setup, the fix might be to specify jsxFactoryFragment: null as in #60122

@RobertSandiford
Copy link
Author

RobertSandiford commented Dec 1, 2024

@iisaduan

What JSX setting are you currently using in your tsconfig? The "requires React to be in scope" sounds like it could be hitting a default error message, so knowing your jsx setting would help me diagnose the issue

Since you are using a custom setup, the fix might be to specify jsxFactoryFragment: null as in #60122

It's just "jsx": "preserve" as the JSX transform is a post-tsc build step.

It seems to me that with "jsx": "preserve" TS should not care about any JSX factories (although there might be something I'm not aware of).

I've tried "jsxFragmentFactory": null, but it makes no difference.

Here is a new playground with "jsx": "preserve" set, this now matches what I see in my IDE: playground 2

Thanks

@RobertSandiford
Copy link
Author

@iisaduan Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants