[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 ...
随机推荐
- Java开发笔记(一百零九)XML报文的定义和解析
前面介绍了JSON格式的报文解析,虽然json串短小精悍,也能有效表达层次结构,但是每个元素只能找到对应的元素值,不能体现更丰富的样式特征.比如某个元素除了要传输它的字符串文本,还想传输该文本的类型. ...
- c++基础(五)——关联容器
1.关联容器 关联容器中的元素时按照关键字来保存和访问的,与之相对的,顺序容器中的元素时按它们在容器中的位置来顺序保存和访问的.两个主要关联容器是 map 和 set.标准库提供了8个关联容器,这8个 ...
- JOIN的区别
CREATE TABLE `j1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c1` varchar(20) NOT NULL DEFAU ...
- docker深入学习一
docker是一个客户服务器结构的应用程序,其结构如下所示 其组成部分包括 container容器:是image的运行实例,一般container之间以及container与主机之间是相互隔离的,相当 ...
- R学习笔记3 数据处理
1,日期类型 日期类型比较特殊,日期值通常以字符串的形式输入到R中,然后使用as.Date()函数转换为以数值形式存储的日期变量 mydate <- as.Date("2019-01- ...
- SharePoint中用Power shell命令修改文档的创建时间
第一步:pnp组件连接到SharePointConnect-PnpOnline -url 网站地址 第二步:查出文档库及文档库下所有的文件 Get-PnPListItem -List 文档库名称 第三 ...
- 【阅读笔记:散列表】Javascript任何对象都是一个散列表(hash表)!
什么是散列表? 散列表是Dictionary(字典)的一种散列表实现方式,字典传送门 一个很常见的应用是使用散列表来表示对象.Javascript语言内部就是使用散列表来表示每个对象.此时,对象的每个 ...
- UI5-技术篇-Hybrid App-2-Geolocation位置定位
在SAP WEB IDE简单测试基于HTML5自带的定位功能,相关步骤如下: 1.VIEW代码 <mvc:View xmlns:core="sap.ui.core" xmln ...
- Struts框架笔记04_拦截器_标签库
目录 1. Struts2的拦截器 1.1 拦截器概述 1.2 拦截器的实现原理 1.3 Struts的执行流程 1.4 拦截器入门 1.4.1 环境搭建 1.4.2 编写拦截器 1.4.3 配置拦截 ...
- 程序写入mycat中文乱码解决(也包括mysql编码修改)
乱码问题可能出现的三个地方 1.程序连接的编码要设置 jdbc:mysql://192.168.1.1:8066/TESTDB?useUnicode=true&characterEncodin ...