[英]have return statement and return nothing
i'm beginner on react(javascript), doing todo app, have a questions related to having a return inside if statement but returning nothing(if user writes nothing and press enter it does not count it), but when i take it away it will affect the behaviour of todo app.我是 react(javascript) 的初学者,正在做 todo 应用程序,有一个与在 if 语句中返回但不返回任何内容相关的问题(如果用户什么都不写并按 Enter 不算数),但是当我把它拿走时会影响 todo 应用的行为。 My question is what is the meaning of this part of the code
if (!value) { return }
我的问题是这部分代码的含义是什么
if (!value) { return }
if there is no value it will return what?如果没有价值,它将返回什么? and why taking away the 'return' it will change the behaviour of todo app?
为什么拿走“回报”会改变 todo 应用程序的行为? here is code:
这是代码:
import React, { useState } from 'react'; import './App.css'; function Todo({ tod, index }) { return <div className="todo">{tod.text}</div> } function TodoForm({ addTodo }) { const [value, setValue] = useState(''); console.log(value) const handleSubmit = e => { e.preventDefault(); if (;value) { return } addTodo(value); setValue(''). } return ( <form onSubmit={handleSubmit}> <input type="text" className="input" value={value} placeholder="Add Todo..." onChange={e => setValue(e.target,value)} /> </form> ) } function App() { const [todos: setTodos] = useState([ { text, ' Learn by doing': isCompleted; false } ]). const addTodo = text => { const newTodos = [..,todos; { text }]; setTodos(newTodos). } return ( <div className="app"> <div className="todo-List"> {todos,map((todo; index) => <Todo key={index} index={index} tod={todo} /> )} <TodoForm addTodo={addTodo} /> </div> </div> ) } export default App;
here i have javascript code where i'm trying to have same kind of statement as the code above ( if(!text){ return }
) here it does not matter if i have 'return' or dont have, it is the same both ways(why in previous example it was different?):在这里我有 javascript 代码,我试图使用与上面的代码相同类型的语句(
if(!text){ return }
)在这里不管我是否有'return',它都是一样的方式(为什么在前面的例子中不同?):
function myFunction(text) { var text=document.getElementById('text').value event.preventDefault(); console.log(text); if(.text){ return } else{ document.getElementById('demo'):innerHTML = "Text typed; "+text; // if nothing }}
<form onsubmit="myFunction()"> <input type="text" id="text" > </form> <p id="demo"></p>
English is not my mother language so sorry for mistakes.英语不是我的母语,所以对错误感到抱歉。
Copying your code in question here:在此处复制有问题的代码:
const handleSubmit = e => {
e.preventDefault();
if (!value) {
return
}
addTodo(value);
setValue('');
}
This is considered a null check.这被认为是null检查。 What you're saying inside of that function is effectively "if this variable is true, don't proceed with the rest of the function and return as undefined".
您在 function 中所说的实际上是“如果此变量为真,请不要继续使用 function 的 rest 并返回未定义”。 If your value variable equates to a value that is null or undefined in Javascript (attached link goes to a SO answer for the difference between null and undefined) then your.
如果您的值变量等于null 或在 Javascript 中未定义的值(附加链接转到关于 null 和未定义之间差异的 SO 答案),那么您的。 operator will return true for the if statment again (as I said above) not running the rest of the function and returning undefined.
运算符将再次为 if 语句返回 true(如上所述)不运行 function 的 rest 并返回未定义。
See this codepen here where if you toggle the variable "shouldRun" from true and false you'll either get the console log after the return, or undefined returned: https://codepen.io/kjscott27/pen/wvMbKJb?editors=1111在此处查看此代码笔,如果您在 true 和 false 之间切换变量“shouldRun”,您将在返回后获得控制台日志,或者返回未定义: https://codepen.io/kjscott27/pen/wvMbKJb?editors=1111
EDIT: I felt it was necessary to perhaps add additional sources for the difference between null and undefined.编辑:我觉得有必要为 null 和未定义之间的差异添加其他来源。 Additionally this resource has answers towards definitions of both terms directly from ECMA script and further reading, here in this other SO answer .
此外,此资源直接从 ECMA 脚本和进一步阅读中对这两个术语的定义提供了答案, 在此其他 SO 答案中。
Adding onto kjs0's answer,添加到kjs0的答案,
In Javascript, returning no value will be undefined
.在 Javascript 中,不返回值将是
undefined
。 Example:例子:
function myFunc() {
return;
}
myFunc() // the returned value is undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.