[Javascript] Avoid Accidental Returns of New State by using the void Keyword
For example we have a 'useState' function, which takes a state and a function to update the state:
const useState = (state, setState) => {
const newState = setState(state);
if (newState != null) {
return newState;
} else {
return state;
}
};
If the new state is not undefined or null, we will return newState otherwise, we return the original state.
But when we run the code like this:
const res = useState([], state => state.push()); //
We expect the res to be [1, 2], but we got 2, this is because 'push' method return the length of the array as a result.
To solve the problem we can use 'void' keyword, it will execute the expression and return undefined as a result, for example:
void == '' // (void 2) == '2', the same as undefined == '2', which is false
void ( == '') // void false which is undefined
const useState = (state, setState) => {
const newState = setState(state);
if (newState != null) {
return newState;
} else {
return state;
}
};
const res = useState([], state => void state.push());
console.log(res); //[1,2]
[Javascript] Avoid Accidental Returns of New State by using the void Keyword的更多相关文章
- [React] Use CSS Transitions to Avoid a Flash of Loading State
Based on research at Facebook, we know that if a user sees a flash of loading state, they perceive t ...
- Javascript基础系列之(五)关键字和保留字 (keyword)
关键字不可以作为变量名或者函数名 break case catch continue default delete do else finally for function if in instanc ...
- Fragment Transactions & Activity State Loss
转自:http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html The foll ...
- 如何精通javascript
http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...
- JavaScript eval() 为什么使用eval()是一个坏主意 什么时候可以使用eval()
---------------------------------------------------------------------------------------------------- ...
- --@ui-router——$state服务原版详解
$state service in module ui.router.state Description $state service is responsible for representing ...
- Javascript周报#182
This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 182Ma ...
- MVC控制下输出图片、javascript与json格式
/// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionR ...
- 转 创建 JavaScript XML 文档注释
http://www.cnblogs.com/chenxizhang/archive/2009/07/12/1522058.html 如何:创建 JavaScript XML 文档注释 Visual ...
随机推荐
- 解决Jupyter notebook安装后不自动跳转网页的方法
在安装完Jupyter notebook后,有童鞋说出现了各种不友好的问题,鉴于此情况,个人先随手写出以下三种情况,并给出解决方法: 题外建议:请使用谷歌浏览器为默认浏览器 一.对于弹不出浏览器的解决 ...
- Python字符串的截取原理,下标的位置图示
Python字符串截取时总是有些糊涂,从官网上找到一个图示,理解Python字符串是如何标记,的具体含义图示如下: +---+---+---+---+---+---+ | P | y | t | h ...
- JSP页面验证码实现
首先在JSP页面加上生成图片的链接 <img type="image" src="auth/authCode" id="codeImage&qu ...
- iOS核心动画(基础篇)
Core Animation相关内容基本介绍 此框架把屏幕上的内容组合起来,这个内容被分解成图层,放到图层树中,这个树形成了你能在应用程序看到的内容的基础 图层在iOS中就是CALayer类 当我们创 ...
- 微信H5页面前端开发,大多数人都会遇到的几个兼容性坑
最近给公司微信公众号,写了微信h5业务页面,总结分享一下前端开发过程中的几个兼容性坑,项目直接拿的公司页面,所以下文涉及图片都模糊处理了. 1.ios端兼容input光标高度 问题详情描述:input ...
- java之struts2之异常处理
1.在应用项目中,异常的出现时很正常的.而且项目上线后发生异常也很正常的.那么需要对这些异常有相应的处理机制,以便客户能够看你到更加友好的界面.Struts2中提供了异常处理机制. 2.Struts中 ...
- jdk安装以及Java环境配置
jdk其实自己大一的时候就已经装过,java环境也配置过,但是随着后面学习的东西越来越多,要安装的软件也越来越多,一开始没有安装路径的概念,好多东西都放的很乱.接着这次自己复习java的机会,于是重新 ...
- 深入理解JVM(四) -- 垃圾内存回收的判定方法和内容
上一篇文章我们学到了对象在内存中是如何存储的已经是如何被访问的,这篇文章将介绍当内存空间不够时,虚拟机将怎样判定对象可不可以被回收已经哪些地方会发生回收. 垃圾回收主要(不是全部)发生在堆内存中,当一 ...
- CMake配置VTK时Qt5_DIR-NOTFOUND的解决方法
直接给解决方法了,不废话. Qt5的路径,请参考:C:\Program\IDE\Qt\Qt5.13.0\5.13.0\msvc2017_64\lib\cmake\Qt5 参考文章 CMake配置VTK ...
- 【转载】 C#使用Math.Abs返回数值的绝对值
在C#的数值运算中,有时候我们需要计算值类型对象的绝对值,此时需要用到C#的数值计算类Math类中的Abs绝对值函数,Math.Abs绝对值函数一共有7个重载类型,支持decimal.double.f ...