• Stars
    star
    172
  • Rank 221,201 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reduce the React boilerplate.
reactive.macro logo

GitHub Workflow Status

A babel macro that helps you reduce the React boilerplate.

Installation

$ npm install reactive.macro --save

babel-plugin-macros is required before using this package.

If you are using create-react-app, it has included babel-plugin-macros.

Usage

import React from 'react';
import { state, bind } from 'reactive.macro';

export default () => {
  let a = state(1);
  let b = state(2);

  return (
    <div>
      <input type="number" value={bind(a)} />
      <button onClick={b => b += 1} >b+</button>

      <p>{a} + {b} = {a + b}</p>
    </div>
  );
};

See live demo.

Equals:

import React, { useState, useCallback } from 'react';

export default () => {
  const [a, setA] = useState(1);
  const [b, setB] = useState(2);

  return (
    <div>
      <input type="number" value={a} onChange={useCallback(e => setA(e.target.value), [])} />
      <button onClick={b => setB(b + 1)} >b+</button>

      <p>{a} + {b} = {a + b}</p>
    </div>
  );
};

API

state

Declare a state.

Arguments

  • initialState - Initial value.

Example

In component:

const App = () => {
  let count = state(0);

  return <button onClick={() => count + 1}>Clicked {count} {count > 1 ? 'times' : 'time'}</button>
}

In custom hook:

const useToggle = () => {
  let visible = state(false);

  const toggle = () => {
    visible = !visible
  }

  return [visible, toggle];
}

You can update the value of count directly without calling setState.

Note: using array methods like push and splice won't trigger re-render. Instead you can use spread syntax.

let users = state([]);

const addUser = (user) => {
  users = [...users, user];
}

bind

Bind a state to a form control.

  • state - The state which is declared by state macro.

Example

const App = () => {
  let name = state('');

  return (
    <div>
      <input value={bind(name)} />
      Hello {name}!
    </div>
  );
}

License

MIT

More Repositories

1

jquery-jsonview

[UNMAINTAINED]View JSON in a more readable format
CoffeeScript
488
star
2

waque

[不再维护]同步本地 Markdown 文档到语雀
TypeScript
244
star
3

remix-routes

Typesafe routing for your Remix apps.
TypeScript
206
star
4

react-with-hooks

[OUTDATED]Ponyfill for the React Hooks API (Support RN)
JavaScript
152
star
5

redux-modal

Redux based modal
TypeScript
105
star
6

mini-store

A minimal state store for React component.
TypeScript
100
star
7

vs-codesandbox

Bring CodeSandbox into your favorite editor
TypeScript
81
star
8

camas

Minimal React authorization library
TypeScript
66
star
9

tmuxrc

My configuration for tmux.
Shell
65
star
10

webpack-less-theme-plugin

Inject less variables to your less file. Support HMR.
JavaScript
26
star
11

styled-antd

Proof of concept using styled-components with antd
JavaScript
25
star
12

react-context-io

Share state with ease.
TypeScript
22
star
13

hexdocset

Convert hex doc to Dash.app's docset format.
Elixir
21
star
14

babel-plugin-reactive

Make React Reactive Again!
TypeScript
18
star
15

rx-hackernews

HackerNews clone built with React/Redux/redux-observable.
JavaScript
17
star
16

dotbox

Backup your dotfiles to dropbox and restore them easily.
Ruby
10
star
17

antd.macro

A babel macro that helps you import antd's style.
TypeScript
9
star
18

antd-rtl

antd rtl demo
CSS
6
star
19

star

下载豆瓣红心歌曲
Ruby
6
star
20

toby

Ruby 版淘宝 API SDK
Ruby
6
star
21

redux-polymorphic

Reuse your reduers
JavaScript
5
star
22

umi-routes-drawer

TypeScript
5
star
23

yesmeck

5
star
24

HelloSketch

Swift
4
star
25

douban_fm_hotkey

Douban FM 全局快捷键
JavaScript
4
star
26

react-full-viewport

A React component that sets its children to full viewport.
TypeScript
4
star
27

antd-dash

JavaScript
3
star
28

neotable

JavaScript
3
star
29

formsy-react-inputs

A set of formsy-react inputs without any fancy.
JavaScript
3
star
30

git-phpcs-hooks

Shell
2
star
31

github-comment-webhook

Ruby
2
star
32

vim-tips.org

The source for vim-tips.org.
Ruby
2
star
33

is-test

Next.js template for CodeSandbox Projects
TypeScript
2
star
34

monkey-lang

A Monkey implementation in Rust for learning purpose.
Rust
2
star
35

bookmarklets

Chrome bookmarklet extension
JavaScript
2
star
36

babel-preset-antd

JavaScript
2
star
37

git-graph

Graph git commits.
CSS
1
star
38

tree-transfer

JavaScript
1
star
39

react-vue-antd-demo

JavaScript
1
star
40

babel-spread-undefined

JavaScript
1
star
41

huami.ex

A CLI version of flower password writing in Elixir.
Elixir
1
star
42

enzyme-set-props-bug

JavaScript
1
star
43

hello-codesandbox-projects

Remix template for CodeSandbox Projects
TypeScript
1
star
44

bug-24

JavaScript
1
star
45

antd-enzyme

[DEPRECATED]
JavaScript
1
star
46

hello-form-ios

Next.js template for CodeSandbox Projects
TypeScript
1
star
47

vimrc

My very personal vimrc
Vim Script
1
star
48

Sublime-IDE

My configuration for using Sublime Text as an IDE.
Python
1
star
49

huami.rb

A CLI version of flower password.
Ruby
1
star
50

hubot-tianqi

Hubot 天气查询
CoffeeScript
1
star
51

raycast-extensions

My Raycast extensions
TypeScript
1
star
52

hubot-xiexie

A hubot script that accepts your 谢谢
CoffeeScript
1
star
53

remax-suspense-demo

JavaScript
1
star
54

github-slideshow

A robot powered training repository 🤖
HTML
1
star
55

react-oom

JavaScript
1
star
56

babel-plugin-resolve-es6-module

JavaScript
1
star
57

tips.vim

Show vim tips everytime when you enter vim.
Vim Script
1
star