# babel-plugin-react-label-sugar

> babel plugin to use label sugar for useState hook

Latest version **0.1.0** (published 2020-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-react-label-sugar
pnpm add babel-plugin-react-label-sugar
yarn add babel-plugin-react-label-sugar
bun add babel-plugin-react-label-sugar
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-11-14 |
| First published | 2020-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Yiyang Xu |
| Maintainers | infinitex |
| Keywords | babel, plugin, react, react hooks, label sugar |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-react-label-sugar
- Repository: https://github.com/InfiniteXyy/babel-plugin-react-label-sugar
- Homepage: https://github.com/InfiniteXyy/babel-plugin-react-label-sugar#readme
- Issues: https://github.com/InfiniteXyy/babel-plugin-react-label-sugar/issues
- npm.io page: https://npm.io/package/babel-plugin-react-label-sugar

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-11-14
- 0.1.0-alpha.4 — 2020-11-13
- 0.1.0-alpha.3 — 2020-11-12
- 0.1.0-alpha.2 — 2020-11-12
- 0.1.0-alpha.1 — 2020-11-12
- 0.1.0-alpha — 2020-11-12
- 0.0.2 — 2020-11-12
- 0.0.1 — 2020-11-12

## README

## babel-plugin-react-label-sugar

A simple React Label Sugar just for fun :) 😄

online babel [playground](https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&spec=false&loose=false&code_lz=GYVwdgxgLglg9mABAQQA6oBQEpEG8BQiiATgKbABciEc4UiAvIgAwDchiHA7gIZQQALKgBNaAIwA2pYY2q0w9BgD45dRACpEAJnZFufQSPFSZyvByI0wAZzhSAdBLgBzDKJCTpWXYgC-HDjIoEGIkAB4xECgoBEQEAGEJGAgAawZcbEYVGjoAalzfJVwchV8wgHpI6IQlfF8gA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=&prettier=true&targets=&version=7.12.3&externalPlugins=babel-plugin-react-label-sugar%400.1.0-alpha.4%2C%40babel%2Fplugin-syntax-jsx%407.12.1)

codesandbox [example](https://codesandbox.io/s/babel-react-label-sugar-example-ifuo2)

### Quick Start
```sh
# install
npm install --save-dev babel-plugin-react-label-sugar
```

Add plugin in .babelrc
```json
{
  "plugins": ["babel-plugin-react-label-sugar"]
}
```

(optional) custom options
```json5
{ 
  "refLabel": "$",              // default is "ref"
  "watchLabel": "on",           // default is "watch"
  "stateFactory": "useState",   // default is "React.useState"
  "memoFactory": "useMemo",     // default is "React.useMemo"
  "effectFactory": "useEffect", // default is "React.useEffect"
  "ignoreMemberExpr": false,    // default is true, will skip object value mutation
}
```

### Examples

```ts
// before
ref: count = 0;
watch: doubled = count => count * 2
watch: count => console.log(count)

count++;
count = 10;
// after
const [count, setCount] = useState(0);
const doubled = useMemo(() => count * 2, [count]);
useEffect(() => {
  console.log(count);
}, [count])

setCount(count => count + 1);
setCount(count => 10);
```

### Q&A

Q: Can I use it to declare objects like `Vue ref`?

A: No, this plugin doesn't do anything on your code, but transpile the label expression to useState function call.

```tsx
// so you must use it like this
$: student = { name: "xyy" };

<input onChange={e => student = { ...student, name: e.target.value }}></input>
```

But you can use immer to achieve this

Set .babelrc `{ "ignoreMemberExpr": false, "stateFactory": "useImmer" }`

```jsx
// before
ref: obj = { count: 0 }
obj.count++;

// after
const [obj, setObj] = useImmer({ count: 0 });
setObj((obj) => { obj.count++ });
```

### Todo List
- [x] support useImmer, transpile `obj.value = 1` to `setObject(obj => obj.value = 1)`
- [x] more labels, auto generate dependency list.
- [ ] custom labels

---
_Source: https://npm.io/package/babel-plugin-react-label-sugar · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
