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的更多相关文章

  1. [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 ...

  2. Javascript基础系列之(五)关键字和保留字 (keyword)

    关键字不可以作为变量名或者函数名 break case catch continue default delete do else finally for function if in instanc ...

  3. Fragment Transactions & Activity State Loss

    转自:http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html The foll ...

  4. 如何精通javascript

    http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...

  5. JavaScript eval() 为什么使用eval()是一个坏主意 什么时候可以使用eval()

    ---------------------------------------------------------------------------------------------------- ...

  6. --@ui-router——$state服务原版详解

    $state service in module ui.router.state Description $state service is responsible for representing ...

  7. Javascript周报#182

    This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 182Ma ...

  8. MVC控制下输出图片、javascript与json格式

    /// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionR ...

  9. 转 创建 JavaScript XML 文档注释

    http://www.cnblogs.com/chenxizhang/archive/2009/07/12/1522058.html 如何:创建 JavaScript XML 文档注释 Visual ...

随机推荐

  1. spring框架学习(二)——注解方式IOC/DI

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  2. Java开发笔记(一百三十五)Swing的文件对话框

    除了常规的提示对话框,还有一种对话框也很常见,它叫做文件对话框.文件对话框又分为两小类:打开文件的对话框.保存文件的对话框,但在Swing中它们都用类型JFileChooser来表达.下面是JFile ...

  3. 11 IO流(八)——装饰器设计模式,Filter装饰流

    声明:本文部分图片及内容引用自:https://www.cnblogs.com/qiumingcheng/p/5219631.html java装饰器设计模式 举一个形象的例子,人可以说话,而扩音器可 ...

  4. Git手册(一):基本操作

    Git小册 本手册参考自runoob及其他网络资源,仅用于学习交流 Git工作流程   一般工作流程   1.克隆 Git 资源作为工作目录.   2.在克隆的资源上添加或修改文件.   3.如果其他 ...

  5. epoll_ctl函数的使用

    #include <sys/epoll.h> int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);作用: ...

  6. python爬虫-《笔趣看》网小说《悟空看私聊》

    小编是个爱看小说的人,哈哈 # -*- coding:UTF-8 -*- ''' 类说明:下载<笔趣看>网小说<悟空看私聊> ''' from bs4 import Beaut ...

  7. tomcat配置外部静态资源映射路径(windows和Linux部署)

    如果你不想用ngnix配置的话,只单独使用tomcat的话可以看看这篇文章,接下来开始 使用场景 1.单机开发有时侯如果放在war下每次clean都会清理当前项目下静态文件特别折腾. 2.只有启动to ...

  8. aspnetcore 容器化部属到阿里云全过程记录

    第一次写博客,作为一个全栈er,记录一下从阿里云到产品运维上线的全过程 一.阿里云上的设置 购买阿里云ECS后: 进控制台查看实例公网IP 在控制台.网络与安全->安全组,配置规则 点击进去可以 ...

  9. iOS - 动态库上架瘦身(去调虚拟机架构),不然验证会报错。

    eg: localhost:改造 M.emor.Y$ lipo WebRTC.framework/WebRTC -thin armv7 -output WebRTC_armv7localhost:改造 ...

  10. [转].net mvc + vuejs 的项目结构

    .net项目结构: 程序目录结构: vue操作: 前提:安装npm ,vue,vue-cli 1.进入控制台窗口 2.进入程序目录 3.运行 vue init webpack webjs 生成webj ...