Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
babel-plugin-mutable-react-state
Advanced tools
> (WIP) Use mutable variable declarations as state in react
(WIP) Use mutable variable declarations as state in react
UNSTABLE The plugin is still under development so isn't recommended for production
Check this issue
function Component() {
let $text = ''
return (
<>
<input
value={$text}
onChange={(e) => {
$text = e.target.value
// some code
// won't work...
$text = $text.toUpperCase()
}}
/>
</>
)
}
// CAN be written as
function Component() {
let $text = ''
return (
<>
<input
value={$text}
onChange={(e) => {
const val = e.target.value
// some code
// will work...
$text = val.toUpperCase()
}}
/>
</>
)
}
// the value of `length` will still be the older value of $text and not the latest one
changeHandler(){
$text = value
$length = $text.length
}
// you'll still have to consider that dependent values need to be handled with useEffect
useEffect(()=>{
$length = $text.length
},[$text])
changeHandler(){
$text = value
}
The plugin assumes you already have jsx
enabled on babel or are using preset-react
in your setup.
npm i babel-plugin-mutable-react-state
# or
yarn add babel-plugin-mutable-react-state
// .babelrc
[
{
"plugins": ["babel-plugin-mutable-react-state"]
}
]
You write state with a prefix $
and that's converted to useState
accordingly.
import * as React from 'react'
function Component() {
let $a = 1
const onPress = () => {
$a += 1
}
return (
<div>
<p>{$a}</p>
<button onClick={onPress}>Press</button>
</div>
)
}
↓ ↓ ↓ ↓ ↓ ↓
import * as React from 'react'
function Component() {
const [a, setA] = React.useState(1)
const onPress = () => {
setA(a + 1)
}
return (
<div>
<p>{a}</p>
<button onClick={onPress}>Press</button>
</div>
)
}
FAQs
> (WIP) Use mutable variable declarations as state in react
The npm package babel-plugin-mutable-react-state receives a total of 2 weekly downloads. As such, babel-plugin-mutable-react-state popularity was classified as not popular.
We found that babel-plugin-mutable-react-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.