npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-react-annotated

v1.0.5

Published

Annotates react statements with flow like annotations (eg. '//@state', //@init) providing for shorter code build up on development

Downloads

16

Readme

babel-plugin-react-annotated

A simple '@' annotation prefixed single line comment that cuts down time wasted on unnecessary instantiation or setting of react's state ( writing less coding more ).

native react's way

/* react state declaration */
const [count, setCount] = React.useState(0);

/* variable usage and setter */
const Component = () => (
  <div onClick={() => setCount(count + 1)}>Counting Number: {count}</div>
);

react-annotated's way

/* react state declaration */

//@state
let count = 0;

/* variable usage */
const Component = () => (
  <div
    onClick={function() {
      ++count;
    }}
  >
    Counting Number: {count}
  </div>
);

Behind the hood

react-annotated is a babel plugin providing transformation of annotated declarations to it's equivalent native react transform on build time, resulting to zero latency effect at run-time.

transformation

//@state
let variable = "a word";

/* transforms to */
const [{ variable }, _SET__variable] = React.useState({ variable: "a word" });

Installation

    npm install --save-dev babel-plugin-react-annotated

Available annotations

  • @state
  • @init

Usage

Note: react-annotated works for only functional react not classes.

Creating a react state

Declaring a react state is as simple as declaring a javascript variable though it's requires;

  • A single line comment annotation with the @ prefix at the top of the declaration (just like a flow annotation).
single state declaration
//@state
let varable = 0;
multiple state declaration
    //@state
    let varable = 0,
	    variable2 = 'string',
	    variable 3 = {},
	    varaible4 = [];

Using a react state

//@state
let variable = 0;

const Component = (props) => {
  return <div>{variable} </div>;
};

/* output */
<div> 0 </div>;

Setting a react state

Setting a react's state is simply assigning a new value to the declared variable just as a usual javascript variable. This can either be

  • An update expression
  • A single assignment expression
  • A nested assignment expression
Update Expression
//@state
let variable = 0;

const Component = (props) => (
  <div
    onClick={function() {
      ++variable;
    }}
  >
    Counting Number: {variable}
  </div>
);
Assignment Expression
//@state
let variable = 0;
//@state
let obj = { keyVar: "" };

const Component = (props) => (
  <div
    onClick={() => {
      variable = "new value as string";
      obj.keyVar = "new value";
    }}
  >
    Counting Number: {variable}
    object value for key (keyVar): {obj.keyVar}
  </div>
);
Nested assignment Expression
//@state
let variable = 0;
//@state
let variable2 = 3;
//@state
let obj = { keyVar: 0 };

let nonState = 5;

const Component = (props) => (
  <div
    onClick={() => {
      obj.keyVar = variable = variable2 = ++nonState;
    }}
  >
    Counting Number 1: {variable}
    Counting Number 2: {variable2}
    Counting Number 3: {obj.keyVar}
  </div>
);

@init annotation

Making a function/arrow function decleration execute at the begining of a component rending is as easy as just annotating the function with @init

//@init
function runInitially() {
  console.log("i ran when the component was mounted and would not run again");
}

if the decleared function takes an argument then the following can be done

//@init('arg1', 3, {}, [])
function runInitiallyWithArguments(stringArg, numberArg, objectArg, arrayArg) {
  console.log(
    "i ran when the component was mounted but with this arguments: ",
    stringArg,
    numberArg,
    objectArg,
    arrayArg
  );
}

Dependencies