[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 ...
随机推荐
- Spring Cloud Stream如何消费自己生产的消息
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- JMeter中文乱码的解决
对于JMeter中文乱码分为两部分: 1.请求(request):由于引用了csv文件当csv文件编码与JMeter不统一可能造成服务器收到数据的中文乱码 2.返回(response):由于服务器返回 ...
- backpropagation algorithm
搞卷积神经网络的时候突然发现自己不清楚神经网络怎么训练了,满脸黑线,借此机会复习一下把. 首先放一位知乎大佬的解释.https://www.zhihu.com/question/27239198?rf ...
- JavaTCP粘包、拆包
import java.nio.ByteBuffer; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBu ...
- 在右键菜单中添加用Jupyter Notebook打开
在右键菜单中添加用Jupyter Notebook打开 为了把 Jupyter 的工作目录指定到某一个文件夹下,每次都要启动 CMD 终端, 然后激活 Anaconda 环境,再启动 Jupyter ...
- BUAA OO 2019 第三单元作业总结
目录 总 JML规格化设计 理论基础 工具链 规格验证 验证代码 代码静态检查 自动生成测试样例 生成结果 错误分析 作业设计 第九次作业 架构 代码实现 第十次作业 架构 代码实现 第十一次作业 架 ...
- 2019年北航OO第三次博客总结
一.JML语言理论基础及其工具链 1. JML语言理论基础 JML是用于对Java程序进行规格化设计的一种表示语言,是一种行为接口规格语言(Behavior Interface Specificati ...
- 巧用CSS3之background渐变
常见斑马loading 上图是我们常见的loading进度条,以前都是用一张背景图片平铺的.其实如果抛去兼容性因素,我们可以用零图片纯样式来实现. 一,首先,我们先为容器定义一个纯蓝色背景: box{ ...
- CPU子系统
CPU的基本结构: CPU的主要部件: 运算部件.缓存部件.寄存器.控制器.时序部件 CPU的工作原理: 主要功能:处理指令.执行操作.控制时间.数据运算 执行指令的流程:读取指令.指令译 ...
- Java 之 Servlet 基础入门
Servlet 一.什么是 Servlet 1.概念 Servlet:server applet,是指运行在服务器端的小程序 2.Servlet servlet 就是一个接口,定义了 Java 类 ...