6.0.0 β’ Published 5 months ago
@putout/plugin-react-hooks v6.0.0
@putout/plugin-react-hooks
πPutout plugin adds ability to convert class components to react hooks. Not bundled.
Install
npm i putout @putout/plugin-react-hooks -D
Add .putout.json
with:
{
"plugins": ["react-hooks"]
}
Rules
Here is list of rules:
{
"rules": {
"react-hooks/apply-short-fragment": "on",
"react-hooks/declare": "on",
"react-hooks/rename-method-under-score": "on",
"react-hooks/convert-state-to-hooks": "on",
"react-hooks/remove-bind": "on",
"react-hooks/remove-this": "on",
"react-hooks/remove-react": "on",
"react-hooks/convert-class-to-function": "on",
"react-hooks/convert-component-to-use-state": "on",
"react-hooks/convert-import-component-to-use-state": "on"
}
}
apply-short-fragment
Apply shorthand syntax of Fragment
.
Check out in πPutout Editor.
β Example of incorrect code
function Columns() {
return (
<Fragment>
<td>Hello</td>
<td>World</td>
</Fragment>
);
}
β Example of correct code
function Columns() {
return (
<>
<td>Hello</td>
<td>World</td>
</>
);
}
Comparison
Linter | Rule | Fix |
---|---|---|
π Putout | react-hooks/apply-short-fragment | β |
β£ ESLint | react/jsx-fragments | β |
declare
Declare hooks according to Hooks API Reference.
β Example of incorrect code
function Example() {
const [count, setCount] = useState(0);
return (
<div/>
);
}
β Example of correct code
import {useState} from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div/>
);
}
remove-react
Remove import of React
in a similar to react-codemod way.
β Example of incorrect code
import React, {useState} from 'react';
β Example of correct code
import {useState} from 'react';
Example
Consider example using class
:
import React, {Component} from 'react';
export default class Button extends Component {
constructor() {
super();
this.state = {
enabled: true,
};
this.toggle = this._toggle.bind(this);
}
_toggle() {
this.setState({
enabled: false,
});
}
render() {
const {enabled} = this.state;
return (
<button
enabled={enabled}
onClick={this.toggle}
/>
);
}
}
After putout --fix
transform, you will receive:
import React, {useState} from 'react';
export default function Button() {
const [enabled, setEnabled] = useState(true);
function toggle() {
setEnabled(false);
}
return (
<button
enabled={enabled}
onClick={toggle}
/>
);
}
License
MIT
6.0.0
5 months ago
5.1.0
1 year ago
5.0.0
1 year ago
4.8.1
1 year ago
4.8.0
2 years ago
4.5.0
2 years ago
4.4.0
2 years ago
4.7.0
2 years ago
4.6.0
2 years ago
4.1.0
2 years ago
4.3.0
2 years ago
4.2.0
2 years ago
4.0.0
2 years ago
3.1.0
3 years ago
3.0.2
4 years ago
3.0.1
4 years ago
3.0.0
4 years ago
2.0.0
5 years ago
1.4.2
5 years ago
1.4.1
5 years ago
1.4.0
5 years ago
1.3.0
5 years ago
1.2.1
5 years ago
1.2.0
5 years ago
1.1.0
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago