Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
(WIP) transform mutable variables to react-state
License
NotificationsYou must be signed in to change notification settings
barelyhuman/babel-plugin-mutable-react-state
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
(WIP) Use mutable variable declarations as state in react
UNSTABLEThe plugin is still under development so isn't recommended for production
Checkthis issue
- 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.
functionComponent(){let$text=''return(<><inputvalue={$text}onChange={(e)=>{$text=e.target.value// some code// won't work...$text=$text.toUpperCase()}}/></>)}// CAN be written asfunctionComponent(){let$text=''return(<><inputvalue={$text}onChange={(e)=>{constval=e.target.value// some code// will work...$text=val.toUpperCase()}}/></>)}
- This is still react state so you cannot do dependent state updates at once,
// the value of `length` will still be the older value of $text and not the latest onechangeHandler(){$text=value$length=$text.length}// you'll still have to consider that dependent values need to be handled with useEffectuseEffect(()=>{$length=$text.length},[$text])changeHandler(){$text=value}
The plugin assumes you already havejsx enabled on babel or are usingpreset-react in your setup.
npm i babel-plugin-mutable-react-state# oryarn add babel-plugin-mutable-react-state// .babelrc[ {"plugins": ["babel-plugin-mutable-react-state"] }]
You write state with a prefix$ and that's converted touseState accordingly.
import*asReactfrom'react'functionComponent(){let$a=1constonPress=()=>{$a+=1}return(<div><p>{$a}</p><buttononClick={onPress}>Press</button></div>)}↓↓↓↓↓↓import*asReactfrom'react'functionComponent(){const[a,setA]=React.useState(1)constonPress=()=>{setA(a+1)}return(<div><p>{a}</p><buttononClick={onPress}>Press</button></div>)}
About
(WIP) transform mutable variables to react-state
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.