[Javascript] Avoid Accidental Returns of New State by using the void Keyword
JavaScript The of by Using New State keyword
2023-09-14 08:59:14 时间
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([1], state => state.push(2)); // 2
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 2 == '2' // (void 2) == '2', the same as undefined == '2', which is false void (2 == '2') // void false which is undefined
const useState = (state, setState) => { const newState = setState(state); if (newState != null) { return newState; } else { return state; } }; const res = useState([1], state => void state.push(2)); console.log(res); //[1,2]
相关文章
- 解决The HTTP request is not acceptable for the requested resource
- Error fetching command 'build_solr_schema': The 'solr' backend requires the installation of 'pysolr'
- The unit caddy.service has entered the 'failed' state with result 'exit-code'
- ORA-22955: The cardinality parameter is not within the allowed limits ORACLE 报错 故障修复 远程处理
- ORA-27611: Smart I/O failed due to a block corruption detected on the host. The block was received from cell “string”. disk: “string”, block: “string”, disk offset: “string” ORACLE 报错 故障修复 远程处理
- ORA-46011: The value of the “selectPrivilege” element is too long. ORACLE 报错 故障修复 远程处理
- ORA-46014: The value of the “aclFile” element is too long. ORACLE 报错 故障修复 远程处理
- ORA-48444: The single “.” and “*” cannot appear in the middle of the path ORACLE 报错 故障修复 远程处理
- ORA-48489: The input exceeds the maximum length [string] ORACLE 报错 故障修复 远程处理
- ORA-48499: The value of the keyword “string” exceeds the maximum length string ORACLE 报错 故障修复 远程处理
- ORA-53010: The value of the tag is null. ORACLE 报错 故障修复 远程处理
- ORA-53035: The rows for the mapping document (string) do not exist. ORACLE 报错 故障修复 远程处理
- ORA-53410: The attribute string does not conform to the VM rule. ORACLE 报错 故障修复 远程处理
- ORA-13615: The task or object string is greater than the maximum allowable length of 30 characters. ORACLE 报错 故障修复 远程处理
- JavaScript学习总结(九)——Javascript面向(基于)对象编程详解编程语言
- etMySQL and JavaScript—Secure the Future of the Internet(jsmysqlin)
- The Power of Linux: Unleashing the Potential of OpenSource Software(linux英文)
- 界面上的文字表达技巧(The Language of the Interface)
- 「Linux 录屏软件下载」功能强大,操作简单!(Note: The translation of the title can be read as Linux Screen Recording Software Download: Powerful Functionality Simple Operation! (linux录屏软件下载)
- Exploring the Dynamic Duo: The Power of Solr MongoDB Integration(solrmongodb)
- Exploring the Power of MongoDB: The Definitive Guide to Upgrading Arrays(mongodb更新数组)
- Exploring the Power of as86 Linux: The Ultimate Guide for Beginners!(as86linux)
- 优化技巧 (Note The title provided by the AI is incomplete Heres a suggestion for a title that completes the phrase)
- 尝试攻克決勝難關The A to Z of Redis(the a什么redis)
- Javascript常用运算符(Operators)-javascript基础教程