Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

(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

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

(WIP) Use mutable variable declarations as state in react

Test

UNSTABLEThe plugin is still under development so isn't recommended for production

Caveats (for now)

Checkthis issue

Docs

Web Documentation

Notes

  • 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}

Install

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"]  }]

Usage

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>)}

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp