Reactjs 什么是;没有从render返回任何内容”;什么意思?

Reactjs 什么是;没有从render返回任何内容”;什么意思?,reactjs,Reactjs,我写了一个简短的应用程序来刷新每秒的时间,但它不起作用 function add() { const element = ( <h1> hello, {formatName(user)} <h2> The time is {(new.Date().toLocaleTimeString())} </h2> </h1> ); setInterval(add,

我写了一个简短的应用程序来刷新每秒的时间,但它不起作用

function add() {
  const element = (

    <h1>
      hello, {formatName(user)}
        <h2>
          The time is {(new.Date().toLocaleTimeString())}
        </h2>
    </h1>

  );
  setInterval(add, 1000);
}

// Render function
function App() {
  return (
    add();
  );
}

export default App;
setInterval(App, 1000);
函数添加(){
常量元素=(
您好,{formatName(user)}
时间是{(new.Date().toLocaleTimeString())}
);
设置间隔(加上1000);
}
//渲染功能
函数App(){
返回(
添加();
);
}
导出默认应用程序;
设定间隔(App,1000);
以下是我得到的错误: 应用程序(…):渲染未返回任何内容。这通常意味着缺少返回语句。或者,要不呈现任何内容,请返回null。

App()函数将返回add()返回的数据。由于函数add()只声明一个const,然后再次调用自身,并且没有返回任何数据,因此最终将不会返回任何内容,因此会出现错误。如果在该函数的末尾添加一个返回值(我猜该值将是元素变量),它应该可以工作

我还注意到,运行代码时可能会出现运行时错误,因为您以每秒调用
App()
的间隔开始
App()
然后每秒调用
add()
,它每秒调用自己。这将创建
n
间隔量,间隔量根据程序运行的时间呈指数增长,最终自行崩溃

解决这个问题取决于您希望这个程序如何运行,因为您还没有完全清楚地说明您要做什么。但是如果我们假设您只是想每秒运行一次函数,那么删除
add()
中的
setInterval()
函数将解决这个问题

固定代码

function add() {
  const element = (

    <h1>
      hello, {formatName(user)}
        <h2>
          The time is {(new.Date().toLocaleTimeString())}
        </h2>
    </h1>

  );
  // setInterval(add, 1000); Remove this to avoid runtime error
  return element; /* A return value is needed if 
  function App() itself is returning this function, that means
  whichever value this function returns is returned to App.
  If there is no return, then nothing is sent back,
  therefore "nothing" was returned from render */
}

// Render function
function App() {
  return (
    add();
  );
}

export default App;
setInterval(App, 1000);
函数添加(){
常量元素=(
您好,{formatName(user)}
时间是{(new.Date().toLocaleTimeString())}
);
//setInterval(add,1000);删除此选项以避免运行时错误
返回元素;/*如果需要,则需要返回值
函数App()本身返回这个函数,这意味着
此函数返回的值将返回给应用程序。
如果没有返回,则不会返回任何内容,
因此,从render返回“nothing”*/
}
//渲染功能
函数App(){
返回(
添加();
);
}
导出默认应用程序;
设定间隔(App,1000);

Hey Adegen——欢迎来到stackoverflow!通常不应该直接包含代码本身,尤其是当代码这么短的时候。您好,我已经更新了我的答案。希望确保您的问题已经解决。注意:这是JavaScript,不是Java。JavaScript和Java是两种不同的语言,尽管名称相似。