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