简体   繁体   English

React.js - 接收错误“渲染未返回任何内容”。 来自 switch 语句

[英]React.js - Receiving error “Nothing was returned from render.” from switch statement

Given a component which takes in a prop and should render a piece of markup depending on the block.type value, I'm receiving the error给定一个接受道具并且应该根据block.type值呈现一段标记的block.type ,我收到错误

Error: Paragraph(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null. . .

What am I missing in my render I can't get the paragraph to render?我在渲染中缺少什么我无法渲染段落?

const Paragraph = props => {
    const { block  } = props;
    const align = block.attributes.align;
       switch (block.type) {
          case 'CORE_PARAGRAPH':
            if (align === 'center') {
                return (
                    <div style={{ textAlign: align }}>
                        <div>
                            <p
                                dangerouslySetInnerHTML={{ __html: block.innerHtml }}
                            />
                        </div>
                    </div>
                );
            }
            break;
          default:
            break;
        }
 };

If your component don't need to return a "html", think about returning null on default in switch, like this:如果您的组件不需要返回“html”,请考虑在 switch 中默认返回 null,如下所示:

const Paragraph = (props) => {
  const { block } = props;
  const align = block.attributes.align;
  switch (block.type) {
    case "CORE_PARAGRAPH":
      if (align === "center") {
        return (
          <div style={{ textAlign: align }}>
            <div>
              <p dangerouslySetInnerHTML={{ __html: block.innerHtml }} />
            </div>
          </div>
        );
      } else return null;
    default:
      return null;
  }
};

Create a variable called content and update inside the switch cases then return it :创建一个名为content的变量并在 switch case 中更新,然后返回它:

const Paragraph = props => {
    const { block  } = props;
    const align = block.attributes.align;
  
  let content=null;

       switch (block.type) {
          case 'CORE_PARAGRAPH':
            if (align === 'center') {
                content= (
                    <div style={{ textAlign: align }}>
                        <div>
                            <p
                                dangerouslySetInnerHTML={{ __html: block.innerHtml }}
                            />
                        </div>
                    </div>
                );
            }
            break;
          default:
             content=null
            break;
        }

return <>{content}</>
 };

暂无
暂无

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

相关问题 React.JS 渲染没有返回任何内容 - React.JS Nothing was returned from render 反应错误:渲染没有返回任何内容。 这通常意味着缺少 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 Index.js 错误:UserForm(…):没有从渲染返回。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null - Index.js Error: UserForm(…): 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 React:有一个返回语句,但“没有从渲染中返回任何东西。这通常意味着缺少一个返回语句” - React: Have a return statement but "Nothing was returned from render. This usually means a return statement is missing" 渲染没有返回任何内容。 这通常意味着缺少返回语句 - Nothing was returned from render. This usally means a return statement is missing 渲染没有返回任何内容。 这通常意味着缺少返回语句 - 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 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM