Skip to content

Latest commit

 

History

History
1057 lines (645 loc) · 15.8 KB

react.md

File metadata and controls

1057 lines (645 loc) · 15.8 KB

React

Prevent missing displayName in a React component definition

✅ Enabled (error)

// Bad
/*
const Hello = React.createClass({
	render: function () {
		return (<div>Hello {this.props.name}</div>);
	}
});
*/

Forbid certain propTypes (any, array, object)

❌ Disabled


Enforce boolean attributes notation in JSX

✅ Enabled (error)

// Bad
/*
const Hello = <Hello personal={true} />;
*/

// Good
const Hello = <Hello personal />;

Validate closing bracket location in JSX

✅ Enabled (error)

// Bad
/*
<Hello
	firstName="John"
	lastName="Smith" />;

<Hello
	firstName="John"
	lastName="Smith"
	/>;
*/

// Good
<Hello
	firstName="John"
	lastName="Smith"
/>;

Enforce or disallow spaces inside of curly braces in JSX attributes

✅ Enabled (error)

// Bad
/*
<Hello name={ firstname } />;
<Hello name={ firstname} />;
<Hello name={firstname } />;
*/

// Good
<Hello name={firstname} />;
<Hello name={{ firstname: 'John', lastname: 'Doe' }} />;
<Hello name={
	firstname
}
/>;

enforce spacing around jsx equals signs

✅ Enabled (error)

// Bad
/*
<Hello name = {firstname} />;
<Hello name ={firstname} />;
<Hello name= {firstname} />;
*/

// Good
<Hello name={firstname} />;
<Hello name />;
<Hello {...props} />;

Require that the first prop in a JSX element be on a new line when the element is multiline

✅ Enabled (error)

// Bad
/*
<Hello personal
	prop />;
*/

// Good
<Hello
	personal
	prop
/>;

Enforce event handler naming conventions in JSX

✅ Enabled (error)

// Bad
/*
<MyComponent handleChange={this.handleChange} />;
<MyComponent onChange={this.componentChanged} />;
*/

// Good
<MyComponent onChange={this.handleChange} />;
<MyComponent onChange={this.props.onFoo} />;

Validate props indentation in JSX

✅ Enabled (error)

// Bad
/*
// 2 spaces indentation
<Hello
  firstName="John"
/>;

// no indentation
<Hello
firstName="John"
/>;
*/

// Good
<Hello
	firstName="John"
/>;

enforce JSX indentation

✅ Enabled (error)

// Bad
/*
// 2 spaces indentation
<App>
  <Hello />
</App>;

// no indentation
<App>
<Hello />
</App>;
*/

// Good
// 1 tab indentation
<App>
	<Hello />
</App>;

Validate JSX has key prop when in array or iterator

✅ Enabled (error)

// Bad
/*
[<Hello />, <Hello />, <Hello />];
out.map((x) => <Hello>x</Hello>);
*/

// Good
[<Hello key="1" />, <Hello key="2" />, <Hello key="3" />];
out.map((x, i) => <Hello key={i}>x</Hello>);

Limit maximum of props on a single line in JSX

✅ Enabled (error)

// Bad
/*
<Hello firstName="John" lastName="Smith" tel={5555555} />;
*/

// Good
<Hello firstName="John" lastName="Smith" />;
<Hello
	firstName="John"
	lastName="Smith"
	tel={5555555}
/>;

Prevent usage of .bind() in JSX props

✅ Enabled (error)

// Bad
/*
<div onClick={this.handleClick.bind(this)}></div>;
*/

// Good
<div onClick={this.handleClick}></div>;

Prevent duplicate props in JSX

✅ Enabled (error)

// Bad
/*
<Hello name="John" name="John" />;
*/

// Good
<Hello firstname="John" lastname="Doe" />;

Prevent usage of unwrapped JSX strings

❌ Disabled

// Bad
const A = <div>test</div>;

// Good
const B = <div>{test}</div>;

disallow target="_blank" on links

❌ Disabled

// Bad
const Hello = <a target="_blank"></a>;

Disallow undeclared variables in JSX

✅ Enabled (error)

// Bad
/*
<Hello name="John" />;
*/

// Good
const Hello2 = require('./Hello');
<Hello2 name="John" />;

Enforce PascalCase for user-defined JSX components

✅ Enabled (error)

// Bad
/*
<Test_component />;
<TEST_COMPONENT />;
*/

// Good
<TestComponent />;

specify whether double or single quotes should be used in JSX attributes

✅ Enabled (error)


Enforce props alphabetical sorting

✅ Enabled (error)

// Bad
/*
<Hello
	active
	onClick={this.handleClick}
	tel={5555555}
	validate
	firstName="John"
	lastName="Smith"
/>;
*/

// Good
<Hello
	active
	validate
	firstName="John"
	lastName="Smith"
	tel={5555555}
	onClick={this.handleClick}
/>;

Enforce spaces before the closing bracket of self-closing JSX elements

✅ Enabled (error)

// Bad
/*
<Hello/>;
<Hello firstName="John"/>;
*/

// Good
<Hello />;
<Hello firstName="John" />;

Prevent React to be incorrectly marked as unused

✅ Enabled (error)

const React = require('react');

Prevent variables used in JSX to be incorrectly marked as unused

✅ Enabled (error)


Prevent usage of dangerous JSX properties

❌ Disabled

const Hello = (<div dangerouslySetInnerHTML={{ __html: 'Hello World' }}></div>);

Prevent usage of deprecated methods

✅ Enabled (error)

// Bad
/*
React.render(<MyComponent />, root);
React.unmountComponentAtNode(root);
React.findDOMNode(this.refs.foo);
React.renderToString(<MyComponent />);
React.renderToStaticMarkup(<MyComponent />);
*/

Prevent usage of setState in componentDidMount

✅ Enabled (error)

// Bad
/*
class Hello extends Component {
	componentDidMount() {
		this.setState({
 			isLoading: true
		});
	}
	render() {
		return <div>Hello {this.state.name}</div>;
	}
}
*/

// Good
class Hello extends Component {
	constructor() {
		// initialState
		this.state = {
			isLoading: true,
		};
	}
	render() {
		return <div>Hello {this.state.name}</div>;
	}
}

Prevent usage of setState in componentDidUpdate

✅ Enabled (error)

// Bad
/*
const Hello = React.createClass({
	componentDidUpdate() {
		this.setState({
			name: this.props.name.toUpperCase()
		});
	},
	render() {
		return <div>Hello {this.state.name}</div>;
	}
});
*/

Prevent direct mutation of this.state

❌ Disabled

// Bad
/*
class Hello extends Component {
	constructor() {
		this.state = {
			isOpen: false
		};
	}
	handleClick() {
		this.state.isOpen = true;
	}
	render() {
		return <div onClick={this.handleClick}>Hello</div>;
	}
}
*/

// Good
class Hello extends Component {
	constructor() {
		this.state = {
			isOpen: false,
		};
	}
	handleClick() {
		this.setState({
			isOpen: true,
		});
	}
	render() {
		return <div onClick={this.handleClick}>Hello</div>;
	}
}

Prevent usage of isMounted

✅ Enabled (error)

// Bad
/*
const Hello = React.createClass({
	handleClick() {
		if (this.isMounted()) {
			// do something
		}
	},
	render() {
		return <div onClick={this.handleClick}>Hello</div>;
	}
});
*/

Prevent multiple component definition per file

✅ Enabled (error)

// Bad
/*
const Hello = React.createClass({
	render() {
		return <div>Hello {this.props.name}</div>;
	}
});

const HelloJohn = React.createClass({
	render() {
		return <Hello name="John" />;
	}
});
*/

Prevent usage of setState

❌ Disabled


Prevent using string references

❌ Disabled


Prevent usage of unknown DOM property

✅ Enabled (error)

// Bad
/*
const Hello = <div class="hello">Hello World</div>;
*/

// Good
const Hello = <div className="hello">Hello World</div>;

Require ES6 class declarations over React.createClass

✅ Enabled (error)

// Bad
/*
const Hello = React.createClass({
	render: function () {
		return <div>Hello {this.props.name}</div>;
	}
});
*/

// Good
class Hello extends React.Component {
	render() {
		return <div>Hello {this.props.name}</div>;
	}
}

Require stateless functions when not using lifecycle methods, setState or ref

✅ Enabled (error)

// Bad
/*
class Foo extends React.Component {
	render() {
		if (!this.props.foo) {
			return null;
		}
		return <div>{this.props.foo}</div>;
	}
}
*/

// Good
const Foo = function (props) {
	return <div>{props.foo}</div>;
};

Prevent missing props validation in a React component definition

✅ Enabled (error)

// Bad
/*
class Hello extends Component {
	render() {
		return <div>Hello {this.props.name}</div>;
	}
});
*/

// Good
class Hello extends Component {
	render() {
		return <div>Hello {this.props.name}</div>;
	}
}
Hello.propTypes = {
	name: PropTypes.string.isRequired,
};

Prevent missing React when using JSX

✅ Enabled (error)

// Good
const React = require('react');

Restrict file extensions that may be required

❌ Disabled


Require render() methods to return something

✅ Enabled (error)

// Bad
/*
class Hello extends React.Component {
	render() {
		<div>Hello</div>;
	}
}
*/

// Good
class Hello extends React.Component {
	render() {
		return <div>Hello</div>;
	}
}

Prevent extra closing tags for components without children

✅ Enabled (error)

// Bad
/*
const HelloJohn = <Hello name="John"></Hello>;
*/

// Good
const HelloJohn = <Hello name="John" />;

Enforce component methods order

✅ Enabled (error)

// Good
class Hello extends React.Component {

	// Static
	static isAllowed() {}

	// Lifecycle
	static getDerivedStateFromProps(props, state) {}
	componentDidMount() {}
	shouldComponentUpdate() {}
	getSnapshotBeforeUpdate() {}
	componentDidUpdate() {}
	componentWillUnmount() {}

	// Handle
	handleClick() {}

	// get set
	get test() {}
	set test(value) {}

	// everything else
	onClick() {}

	// render*
	renderButton() {}

	// render
	render() {
		return <div>{this.renderButton()}</div>;
	}
}

Enforce propTypes declarations alphabetical sorting

✅ Enabled (error)

Test.propTypes = {
	ab: PropTypes.object.isRequired,
	aa: PropTypes.object,
	b: PropTypes.object,
	x: PropTypes.object,
	onClick: PropTypes.func,
};

Test2.propTypes = {
	ab: PropTypes.object.isRequired,
	onHover: PropTypes.func.isRequired,
	aa: PropTypes.object,
	b: PropTypes.object,
	x: PropTypes.object,
	onClick: PropTypes.func,
};

Prevent missing parentheses around multilines JSX

✅ Enabled (error)