New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[译]React Component最佳实践 #7
Labels
Comments
很有用,感谢分享。 |
来了😏 |
Open
对于第三点:3避免箭头函数写法 const Child = ({ match }) => ( ID: {match.params.id}有什么性能差异吗? |
组件为匿名函数,在调试时,组件可能会显示为<> |
@YutHelloWorld 谢谢,原来是这样 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Class写法
如果组件带有
state
或者方法,就使用Class写法。1. 引入CSS
2. 初始化State
使用ES7句法定义
state
3. 初始化
propTypes
和defaultProps
propTypes
和defaultProps
的声明应该置顶便于其他开发者阅读。在React v15.3.0版本,推荐使用prop-types这个包替代React.PropTypes。重要的一点:所有的组件都应当有propTypes验证。
4. 组件内的方法
在方法中使用箭头函数来替代
this.handleExpand.bind(this)
5.
this.setState()
是异步的。应该使用函数入参6. 一个组件或者元素含有多个
props
应当分行写7. 避免在子组件中使用闭包
8.完整的
class
组件写法函数式组件写法
1. propTypes
2. Destructuring Props and defaultProps
结合ES6的函数入参解构,可以如下书写
3. 避免箭头函数写法
虽然语法没问题,但是这里函数是匿名函数。
4. 高阶组件
5. 完整代码
The text was updated successfully, but these errors were encountered: