|
| 1 | +## Documentation |
| 2 | + |
| 3 | +### Getting Started |
| 4 | + |
| 5 | +Just like any other babel plugin, you install it and then add it to your `.babelrc` |
| 6 | + |
| 7 | +```sh |
| 8 | +npm i -D babel-plugin-mutable-react-state |
| 9 | +# or |
| 10 | +yarn add -D babel-plugin-mutable-react-state</pre> |
| 11 | +``` |
| 12 | + |
| 13 | +```json |
| 14 | +// .babelrc |
| 15 | +{ |
| 16 | + "plugins": ["babel-plugin-mutable-react-state"] |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +### Usage |
| 21 | + |
| 22 | +As shown in the visual example in features, the basic usage involves adding a `$` prefix to your state variables. |
| 23 | +The plugin will then convert it to a **state value,setter pair** |
| 24 | + |
| 25 | +```js |
| 26 | +function Component() { |
| 27 | + let $count = 1 |
| 28 | +} |
| 29 | +// ↓ ↓ ↓ |
| 30 | + |
| 31 | +function Component() { |
| 32 | + const [count, setCount] = React.useState(1) |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Advanced Usage and Workflows |
| 37 | + |
| 38 | +1. You have to realise that this is still a react contextual state and should be treated as one. |
| 39 | + |
| 40 | +```js |
| 41 | +function Component() { |
| 42 | + let $text = '' |
| 43 | + let $textLength = 0 |
| 44 | + |
| 45 | + // handle dependent state updates in useEffect |
| 46 | + React.useEffect(() => { |
| 47 | + $textLength = $text.length |
| 48 | + }, [$text]) |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <textarea |
| 53 | + onChange={(e) => { |
| 54 | + $text = e.target.value |
| 55 | + }} |
| 56 | + /> |
| 57 | + </> |
| 58 | + ) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +2. The current implementation of the plugin does support **self-dependent** state updates |
| 63 | + |
| 64 | +<small class="text-accent">(Added in: v0.0.2)</small> |
| 65 | + |
| 66 | +```js |
| 67 | +function Component() { |
| 68 | + let $text = '' |
| 69 | + |
| 70 | + const onChange = (e) => { |
| 71 | + // update the value |
| 72 | + $text = e.target.value //=> eg: `hello` |
| 73 | + // convert the new value to upper case |
| 74 | + $text = $text.toUpperCase() //=> HELLO |
| 75 | + } |
| 76 | + |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <textarea onChange={onChange} /> |
| 80 | + </> |
| 81 | + ) |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +3. Working with numbers is simple and works as any mutable number variable |
| 86 | + |
| 87 | +``` |
| 88 | +function Component() { |
| 89 | + let $count = 1; // declare a reactive variable using the `$` prefix |
| 90 | +
|
| 91 | + const onUpdate = () => { |
| 92 | + // will increment by 2 |
| 93 | + $count += 2; |
| 94 | +
|
| 95 | + // will set the count to 2 |
| 96 | + $count = 2; |
| 97 | +
|
| 98 | + // will increment by 1 |
| 99 | + $count++; |
| 100 | +
|
| 101 | + // will decrement by 1 |
| 102 | + $count--; |
| 103 | +
|
| 104 | + // will multiply by 1 |
| 105 | + $count *= 2; |
| 106 | + }; |
| 107 | +
|
| 108 | + return <>{$count}</>; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +4. There's obviously cases where you'd want to have a custom arrow function handle the state update `setState((prevState)=>{return newState})`. This can be achieved with a simple arrow function assigned to the state variable |
| 113 | + |
| 114 | +```js |
| 115 | +/* |
| 116 | + naive , but you can basically do anything, |
| 117 | + as long as you return a new value / new instance |
| 118 | +*/ |
| 119 | +$name = (prevName) => prevName.toUpperCase() |
| 120 | +``` |
0 commit comments