博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react es6+ 代码优化之路-1
阅读量:6979 次
发布时间:2019-06-27

本文共 2985 字,大约阅读时间需要 9 分钟。

这里集合了一些代码优化的小技巧

  • 在初步接触 es6+ 和 react 的时候总结的一些让代码跟加简化和可读性更高的写法
  • 大部分知识点是自己在平时项目中还不知道总结的,一致的很多优化的点没有写出来,逐步增加中,目的是使用最少的代码,高效的解决问题
  • 有什么更好的方法和不足之处,欢迎大家指出。

react es6+ 代码优化之路-1

1、函数式默认参数

  • 使用函数默认的参数, 使用 es6+ 的时候,有了结构赋值,我们就不用再函数中自己再去定义一个变量。
/*    当我们使用 es5 的时候**/var es5Fun = function (config) {    var foo = config || 'default foo'    console.log(foo)}//我们传一个对象进去es5Fun() // 'default foo'es5Fun('not default foo') // 'not default foo'/*    当我们使用 es6+ 的时候**/const es6Fun = (config = {
'defalut'})=>{ console.log(config)}es6Fun(); // 'defalut'es6Fun('not defalut') // 'not defalut'复制代码

1.1 简洁的数组解构

//badconst splitLocale = locale.split('-');const language = splitLocale[0];const country = splitLocale[1];//goodconst [language, country] = locale.split('-');复制代码

2、函数命名,布尔变量和返回布尔值的函数应该以is,has,should开头

//bad const good = current => goal;//goodconst isGood = current => goal;复制代码

3、别重复自己的代码

  • 明显可以复用的直接使用组件来套用
//badconst MyComponent = () => (  
);//goodconst MyOtherComponent = ({ type }) => (
);const MyComponent = () => (
);复制代码

3.1、函数和组件传值的复用

  • 当我们开始创建了一个组件 Thingie,到后面我们增加了一个需求,需要随时添加一个 title,所以我们又写了一个组件 ThingieWithTitle。这明显可以复用,下面看一下怎么去复用这个组件在 react 中。
//badimport Title from './Title';export const Thingie = ({ description }) => (  
);export const ThingieWithTitle = ({ title, description }) => (
);复制代码
  • 在这里,我们将 children 传递给 Thingie。然后创建 ThingieWithTitle,这个组件包含 Thingie,并将 Title 作为其子组件传给 Thingie。这样我们就可以分离的使用两个组件,相互不影响,耦合度小。
//goodimport Title from './Title';export const Thingie = ({ description, children }) => (  
{children}
);export const ThingieWithTitle = ({ title, ...others }) => (
);复制代码

4、多使用无状态组件

  • 从渲染分离有状态的部分
//badclass User extends Component {  state = { loading: true };  render() {    const { loading, user } = this.state;    return loading      ? 
Loading...
:
First name: {user.firstName}
First name: {user.lastName}
...
; } componentDidMount() { fetchUser(this.props.id) .then((user) => { this.setState({ loading: false, user })}) }}//good//我们把无状态的组件写进
中import RenderUser from './RenderUser';class User extends Component { state = { loading: true }; render() { const { loading, user } = this.state; return loading ?
:
; } componentDidMount() { fetchUser(this.props.id) .then(user => { this.setState({ loading: false, user })}) }}//anohter goodclass User extends Component { state = { loading: true }; _render(){ return ( ) } render() { const { loading, user } = this.state; return loading ?
:
; } componentDidMount() { fetchUser(this.props.id) .then(user => { this.setState({ loading: false, user })}) }}复制代码

x、使用高阶组件重构代码

  • 未完待续

转载地址:http://jojpl.baihongyu.com/

你可能感兴趣的文章
Java基础-Synchronized原理
查看>>
大道至简,阿里巴巴敏捷教练的电子看板诞生记
查看>>
zookeeper学习04 zookeeper收尾+dubbo前瞻
查看>>
《讲个故事》为什么IP地址与Mac地址 缺一不可
查看>>
华山论剑之浅谈iOS的生产线 工厂模式
查看>>
浅谈javascript异步发展历程
查看>>
在vscode使用editorconfig的正确姿势
查看>>
当心!你的密码正在被手机中的声音泄露!
查看>>
你用过 PropTypes 的这些类型检查么?
查看>>
H5小游戏 【篇一】 组词游戏
查看>>
枚举的使用示例
查看>>
换个姿势学数学:学的爽,用的上
查看>>
runC爆严重漏洞影响Kubernetes、Docker,阿里云修复runC漏洞的公告
查看>>
力扣(LeetCode)146
查看>>
Understanding HBase and BigTable 译文
查看>>
Java™ 教程(泛型、继承和子类型)
查看>>
Spring AOP
查看>>
如何优雅的构建排序公式
查看>>
React手稿之 React-Saga
查看>>
Java基础【七】 - List集合汇总
查看>>