Skip to content

swyxio/react.macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel plugin/macro for react globals

react plugin and macro that lets you write the React APIs as if they were all globals

This:

/*eslint no-undef: 0 */
import React from "react.macro"; //optional if not using babel macro

const ctx = createContext(5);
export default function Timer() {
  const [state, setState] = useState(0);
  const myRef = React.useRef(0);
  return <Suspense>{state}</Suspense>;
}

Translates to:

/*eslint no-undef: 0 react/jsx-no-undef: 0*/
import React from "react";

const ctx = React.createContext(5);
export default function Timer() {
  const [state, setState] = React.useState(0);
  const myRef = React.useRef(0);
  return <React.Suspense>{state}</React.Suspense>;
}

Turning off the ESLint no undef warning is helpful for this.


Creation videos:

Others:


Codesandbox Demo: https://codesandbox.io/s/n5xp37z894

ASTExplorer histories and testing