Skip to content

Commit 235cd0a

Browse files
committed
update notes
1 parent 4981c3b commit 235cd0a

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

README.md

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,71 @@
1111

1212
Check [this issue](https://github.com/barelyhuman/babel-plugin-mutable-react-state/issues/4)
1313

14-
1514
## Notes
16-
- While the caveats exist due to the extensive types of expressions that javascript has, it's recommended that you use a cloned variable and then just assigned the modification to the reactive variable if you plan to use it right now.
15+
16+
- While the caveats exist due to the extensive types of expressions that javascript has, it's recommended that you use a cloned variable and then just assigned the modification to the reactive variable if you plan to use it right now.
17+
1718
```jsx
19+
function Component() {
20+
let $text = ''
1821

19-
function Component(){
20-
let $text = "";
21-
22-
return <>
23-
<input value={$text} onChange={e=> {
24-
$text = e.target.value;
25-
// some code
26-
27-
// won't work...
28-
$text = $text.toUpperCase()
29-
}}/>
30-
</>
22+
return (
23+
<>
24+
<input
25+
value={$text}
26+
onChange={(e) => {
27+
$text = e.target.value
28+
// some code
29+
30+
// won't work...
31+
$text = $text.toUpperCase()
32+
}}
33+
/>
34+
</>
35+
)
3136
}
3237

33-
// CAN be written as
34-
35-
function Component(){
36-
let $text = "";
37-
38-
return <>
39-
<input value={$text} onChange={e=> {
40-
const val = e.target.value;
41-
// some code
42-
43-
// will work...
44-
$text = val.toUpperCase()
45-
}}/>
46-
</>
47-
}
38+
// CAN be written as
4839

40+
function Component() {
41+
let $text = ''
42+
43+
return (
44+
<>
45+
<input
46+
value={$text}
47+
onChange={(e) => {
48+
const val = e.target.value
49+
// some code
50+
51+
// will work...
52+
$text = val.toUpperCase()
53+
}}
54+
/>
55+
</>
56+
)
57+
}
4958
```
5059

60+
- This is still react state so you cannot do dependent state updates at once,
61+
62+
```jsx
63+
// the value of `length` will still be the older value of $text and not the latest one
64+
changeHandler(){
65+
$text = value
66+
$length = $text.length
67+
}
5168

69+
// you'll still have to consider that dependent values need to be handled with useEffect
70+
71+
useEffect(()=>{
72+
$length = $text.length
73+
},[$text])
74+
75+
changeHandler(){
76+
$text = value
77+
}
78+
```
5279

5380
## Install
5481

0 commit comments

Comments
 (0)