简体   繁体   English

有return语句并且什么都不返回

[英]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.

相关问题 React:有一个返回语句,但“没有从渲染中返回任何东西。这通常意味着缺少一个返回语句” - React: Have a return statement but "Nothing was returned from render. This usually means a return statement is missing" return 语句在 javascript 中不返回任何内容 - return statement returns nothing in javascript 在递归中不返回任何内容的 return 语句 - return statement that returns nothing in recursion 为什么 JavaScript 中的这个 return 语句什么都不返回? - Why does this return statement in JavaScript return nothing? 使用不返回任何内容的 return 语句有什么好处吗? - Is there a benefit to using a return statement that returns nothing? 在map返回中if else语句中没有显示任何内容 - Nothing displayed from if else statement in map return Home(…):渲染未返回任何内容。 这通常意味着缺少return语句。 或者,不渲染任何内容,则返回null - Home(…): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null 错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null - Error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null unboundStoryFn(…):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null - unboundStoryFn(…): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null JAVASCRIPT:即使存在返回语句,也没有从渲染返回任何内容 - JAVASCRIPT: Nothing was returned from render even though return statement exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM