[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 ...
随机推荐
- SAS学习笔记58 单元格格式化设计
单元格行_row_ 对于行单元格,主要就通过_row_这么一个自动变量的方式,来对单元格所有行进行格式化设计 例如,对性别为“男”的单元格所在行颜色设定为红色: 单元格列_col_ 将_row_改成_ ...
- Jmeter参数化(_csvread函数、CSV Data Set Config)
方法一.Jmeter自带的函数助手——_CSVRead函数 1.数据准备:先在excel存储数据,保存格式选择csv格式.或在记事本存储数据,列之间用英文逗号分隔,保存为txt 2.使用_csvrea ...
- linux BufferedImage.createGraphics()卡住不动
项目应用服务器tomcat7,在开发(windows).测试环境(linux 64bit)均正常.在生产环境(linux 64bit)一直启动不起来,也没有报错. 最终定位问题:执行到buffered ...
- 导出Excel的2个方法
导出到Excel的两种方法 第一种: 1.首先创建Excle模板,另存为 “xml”文件.使用记事本等编辑软件打开文件的代码.然后另存为视图文件“Export.cshtml”; 2.控制器操作 pub ...
- 创建Core项目使用IdentityServer4
本文主要参照https://www.bilibili.com/video/av42364337/?p=4 英文帮助文档:https://identityserver4.readthedocs.io/e ...
- Java Swing 资料(转载学习)
Swing图像界面简介:https://blog.csdn.net/xietansheng/article/details/72814531 Swing实用经验总结篇:https://blog.csd ...
- ③ Python3.0 数字类型
Python3 的六个标准数据类型中:不可变数据(3 个):Number(数字).String(字符串).Tuple(元组):可变数据(3 个):List(列表).Dictionary(字典).Set ...
- node.js开发 npm包管理工具
npm介绍 说明:npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理依赖等) 使用npm安装插件:命令提示符执行npm instal ...
- SASS系列之:!global VS !deafult
先脑补两组场景. 场景一: 同事们每天中午都会外出吃饭.通常情况下都会先问,去哪儿吃啊?不知道啊?下楼再说吧.到了楼下好不容易有个人站出来说,既然没人说我可就说了啊,咱们去吃香草香草吧.没人反对就去, ...
- css列表滑动防止被底部遮住和适配屏幕长一点的机型处理
1.移动端处理列表滑动的时候,微信底下有自带的返回页面按钮,经常会被遮住,遇到屏幕长一点的,下面会短一大截,以下用此方法可以解决..container{ position:relative; back ...