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. 协议——SCCB与IIC的区别

    SCCB(Serial Camera Control Bus,串行摄像头控制总线)是由OV(OmniVision的简称)公司定义和发展的三线式串行总线,该总线控制着摄像头大部分的功能,包括图像数据格式 ...

  2. idea 代码热启动配置方法

    Restart Server:代码已更新重启tomcat服务器

  3. Gym 102055B Balance of the Force

    大意: $n$个骑士, 第$i$个骑士若加入光明阵营, 那么能力值$L_i$, 加入黑暗阵营, 能力值$D_i$. 给定$m$个限制$(u_i,v_i)$, 表示$u_i,v_i$不能在同一阵营. 求 ...

  4. CCF 2016-04-1 折点计数

    CCF 2016-04-1 折点计数 题目 问题描述 给定n个整数表示一个商店连续n天的销售量.如果某天之前销售量在增长,而后一天销售量减少,则称这一天为折点,反过来如果之前销售量减少而后一天销售量增 ...

  5. 使用Identity Server 4建立Authorization Server

    使用Identity Server 4建立Authorization Server (6) - js(angular5) 客户端 摘要: 预备知识: http://www.cnblogs.com/cg ...

  6. Replace到达地

    string getcstr(string cs)        {            String SplitKey = @"乌孜别克族,柯尔克孜族,维吾尔族, 鄂伦春族, 哈萨克族, ...

  7. ubuntu安装mysql数据库方法

    ubuntu基于linux的免费开源桌面PC操作系统,十分契合英特尔的超极本定位,支持x86.64位和ppc架构.一个比较流行的Linux操作系统,不仅简单易用,而且和Windows相容性非常好.那么 ...

  8. iOS 简化冗余代码

    正在给深圳某家智能家居开发iPad版本,在已经存在的iPhone版上修改,该app的界面采用的是xib.xib相比代码来写界面,快速高效,但是可维护性和可读性太差.言归正传,看到这些代码后,我的心情很 ...

  9. SAP Marketing Cloud功能简述(四) : 线索和客户管理

    这个系列的前三篇文章Grace Dong已经给大家带来了下面的内容: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Clou ...

  10. c# HashTable 类