question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use React TSX tags without having to write "import React from 'react'"

See original GitHub issue

Suggestion

I have to write import React from 'react' every time I use TSX syntax even though I make no explicit mention of the name React in the following lines of code (Of course, TSC output will contain mentions of React but the key is explicit).

It is possible to write JSX tags in Babel’s JavaScript without import React from 'react' by using Babel’s/Webpack’s plugins. A notable example is Next.js.

Therefore I suggest: If user are not mentioning React directly, user shouldn’t have to declare React explicitly. This can be configured in compiler options, something like a combination of --jsxFactoryPath and --jsxModule for example.

Examples

React.js

Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": ["createElement"],
    "jsxModule": "react"
  }
}

index.tsx:

export = <div id='123'>hello</div>

Desired Output

index.js:

var _jsx = require("react").createElement;
module.exports = _jsx("div", { id: "123" }, "hello");

Hyperscript

Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": [],
    "jsxModule": "hyperscript"
  }
}

index.tsx:

export = <div id='123'>hello</div>

Desired Output

var _jsx = require("hyperscript");
module.exports = _jsx("div", { id: "123" }, "hello");

Generic

Ideal Source Code

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "jsxFactoryPath": ["obj1", "obj2", "func"],
    "jsxModule": "pkg/dir1/dir2"
  }
}

index.tsx:

export = <div id='123'>hello</div>

Desired Output

index.js:

var _jsx = require("pkg/dir1/dir2").obj1.obj2.func;
module.exports = _jsx("div", { id: "123" }, "hello");

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
KSXGitHubcommented, Sep 21, 2018

Still not sure why anyone would prefer an array over dot-syntax there.

I said this and this.

1reaction
aleclarsoncommented, Sep 21, 2018

Gotcha, just giving my own opinion. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use React TSX tags without having to write "import ... - GitHub
It is possible to write JSX tags in Babel's JavaScript without import React from 'react' by using Babel's/Webpack's plugins. A notable example ...
Read more >
You no longer need to import React from 'react'
If you use React, import React from 'react' is the first thing that you write in your code. Tagged with javascript, react, jsx....
Read more >
JSX can be used without importing React - Stack Overflow
Thus, we don't need to import React component unless we need to use React explicitly. For eg. import React from 'react' // without...
Read more >
React Without JSX
Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is...
Read more >
Using TypeScript with React - DigitalOcean
This tutorial covers how to use TypeScript with React functional or class-based components. In this post we also make use of the Parcel ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found