简体   繁体   English

React:有一个返回语句,但“没有从渲染中返回任何东西。这通常意味着缺少一个返回语句”

[英]React: Have a return statement but "Nothing was returned from render. This usually means a return statement is missing"

I have a component that I'm currently trying to render.我有一个正在尝试渲染的组件。 I put a return statement but I am still displaying an error.我放了一个返回语句,但我仍然显示错误。

import React, { useState } from 'react';
import moment from 'moment';
import {FaSpaceShuttle} from 'react-icons/fa';

export const TaskDate = ({ setTaskDate, showTaskDate, setShowTaskDate }) => {

  return (
  showTaskDate && (
    <div className="task-date" data-testid="task-date-overlay">
      <ul className="task-date__list">
        <li>
          <div
            onClick={() => {
              setShowTaskDate(false);
              setTaskDate(moment().format('DD/MM/YYYY'));
            }}

          >
            <span>
              <FaSpaceShuttle />
            </span>
            <span>Today</span>
          </div>
        </li>
       
      </ul>
    </div>
    )
  );
};

Any help would be appreciated!任何帮助,将不胜感激!

The problem is that when showTaskDate is undefined you don't render anything, you simply return undefined .问题是当showTaskDateundefined你不渲染任何东西,你只是返回undefined

You can change your return to use conditionals and return null to render nothing if there is no showTaskDate set.如果没有设置showTaskDate ,您可以更改return以使用条件并返回null以不呈现任何内容。

export const TaskDate = ({ setTaskDate, showTaskDate, setShowTaskDate }) => {
  return (
    showTaskDate ? (
      <div className="task-date" data-testid="task-date-overlay">
        <ul className="task-date__list">
         ...
        </ul>
      </div>
    ) : null
  );
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 渲染没有返回任何内容。 这通常意味着缺少返回语句 - Nothing was returned from render. This usually means a return statement is missing 反应:渲染没有返回任何内容。 这通常意味着缺少 return 语句 - React: Nothing was returned from render. This usually means a return statement is missing 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 React Component Array Map-渲染未返回任何内容。 这通常意味着缺少return语句。 或者,不渲染任何内容,则返回null - React Component Array Map - Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null 反应错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null - React Error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null React - 错误:应用程序(...):渲染没有返回任何内容。 这通常意味着缺少返回语句。 或者,不渲染任何内容,返回 null - React - Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null 错误:聊天(...):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null - Error: Chats(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null 出现此错误的学校项目:渲染未返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null - School project with this error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM