Class vs React.createClass
// bad const Listing = React.createClass({ render() { return <div />; } });
// good class Listing extends React.Component { render() { return <div />; } }</pre>
// bad const reservationCard = require('https://www.open-open.com/lib/view/ReservationCard');
// good const ReservationCard = require('https://www.open-open.com/lib/view/ReservationCard');
// bad const ReservationItem = <ReservationCard />;
// good const reservationItem = <ReservationCard />;</pre>
// bad const Footer = require('https://www.open-open.com/lib/view/Footer/Footer.jsx')
// bad const Footer = require('https://www.open-open.com/lib/view/Footer/index.jsx')
// good const Footer = require('https://www.open-open.com/lib/view/Footer')</pre>
// bad export default React.createClass({ displayName: 'ReservationCard', // stuff goes here });
// good export default class ReservationCard extends React.Component { }</pre>
// bad <Foo superLongParam="bar" anotherSuperLongParam="baz" />
// good <Foo superLongParam="bar" anotherSuperLongParam="baz" />
// if props fit in one line then keep it on the same line <Foo bar="bar" />
// children get indented normally <Foo superLongParam="bar" anotherSuperLongParam="baz" > <Spazz /> </Foo></pre>
为什么?因为 JSX 属性不能包含被转移的引号,并且双引号使得如"don't"一样的连接词很容易被输入。常规的 HTML 属性也应该使用双引号而不是单引号,JSX 属性反映了这个约定。
</blockquote>eslint rules: jsx-quotes
// bad <Foo bar='bar' />
// good <Foo bar="bar" />
// bad <Foo style={{ left: "20px" }} />
// good <Foo style={{ left: '20px' }} /></pre>
// bad <Foo/>
// very bad <Foo />
// bad <Foo />
// good <Foo /></pre>
// bad <Foo UserName="hello" phone_number={} />
// good <Foo userName="hello" phoneNumber={} /></pre>
/// bad render() { return <MyComponent className="long body" foo="bar"> <MyChild /> </MyComponent>; }
// good render() { return ( <MyComponent className="long body" foo="bar"> <MyChild /> </MyComponent> ); }
// good, when single line render() { const body = <div>hello</div>; return <MyComponent>{body}</MyComponent>; }</pre>
// bad <Foo className="stuff"></Foo>
// good <Foo className="stuff" /></pre>
// bad <Foo bar="bar" baz="baz" />
// good <Foo bar="bar" baz="baz" /></pre>
// bad React.createClass({ _onClickSubmit() { // do stuff }
// other stuff });
// good class extends React.Component { onClickSubmit() { // do stuff }
// other stuff });</pre>
继承 React.Component 的类的方法遵循下面的顺序
</li> </ul>
render
</li> </ol>
import React, { PropTypes } from 'react';
const propTypes = { id: PropTypes.number.isRequired, url: PropTypes.string.isRequired, text: PropTypes.string, };
const defaultProps = { text: 'Hello World', };
class Link extends React.Component { static methodsAreOk() { return true; }
render() { return <a href=https://www.open-open.com/lib/view/{this.props.url} data-id={this.props.id}>{this.props.text}</a> } }
Link.propTypes = propTypes; Link.defaultProps = defaultProps;
export default Link;</pre>
使用 React.createClass 时,方法顺序如下:
</li> </ul>
eslint rules: react/sort-comp
来自:https://github.com/dwqs/react-style-guide 到此这篇react组件定义(react组件命名规范)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/yd-react-native/78080.html