I am trying to render elements from a JSON schema with the code below, but the children of the paragraph are structured as an array containing React Elements when console-logged. Despite this arrangement, the error mentioned in the title continues to occur, why so?
Am I missing something?
Please go through the code below to understand my plight:
import Image from "@components/Image";
import styles from "../pages/proposals/home.module.sass";
import React, { ReactElement } from "react";
import Editor from "@monaco-editor/react";
import { Tweet } from "react-tweet";
interface ParagraphContent {
type: "paragraph";
value: (string | LinkContent | BoldContent)[];
}
interface H1 {
type: "h1";
value: string;
}
interface H2 {
type: "h2";
value: string;
}
interface H3 {
type: "h3";
value: string;
}
interface H4 {
type: "h4";
value: string;
}
interface ImageContent {
type: "image";
value: string;
alt?: string;
}
interface LinkContent {
type: "link";
value: string;
text: string;
target?: string;
}
interface QuoteContent {
type: "quote";
value: string;
cite?: string;
}
interface TweetContent {
type: "tweet";
value: string;
}
interface CodeContent {
type: "code";
value: string;
lang: string;
}
interface BoldContent {
type: "bold";
value: string;
}
interface CaptionContent {
type: "caption";
value: string;
}
type ContentType =
| ParagraphContent
| ImageContent
| CaptionContent
| CodeContent
| TweetContent
| QuoteContent
| H1
| H2
| H3
| H4;
const renderEngine = (
content: ContentType[],
mode: "light" | "dark"
): React.ReactNode => {
return (
<>
{content.map((item, index) => {
if (item.type === "h1") {
return (
<h1
style={{ marginBottom: "20px", marginTop: "0px" }}
key={index}
className={styles.h1}
>
{item.value}
</h1>
);
}
if (item.type === "h2") {
return (
<h2
style={{ marginBottom: "15px", marginTop: "10px" }}
key={index}
className={styles.h2}
>
{item.value}
</h2>
);
}
if (item.type === "h3") {
return (
<h3
style={{ marginBottom: "10px", marginTop: "5px" }}
key={index}
className={styles.h1}
>
{item.value}
</h3>
);
}
if (item.type === "h4") {
return (
<h4 key={index} className={styles.h1}>
{item.value}
</h4>
);
}
// Error from the if block?
if (item.type === "paragraph") {
let children = item.value.map((innerItem) => {
if (typeof innerItem === "string") {
return innerItem;
} else if (innerItem.type === "link") {
return React.createElement(
"a",
{
className: "fancy-link",
href: innerItem.value,
target: innerItem.target || "_blank",
},
` ${innerItem.text} `
);
} else if (innerItem.type === "bold") {
return React.createElement(
"strong",
{},
` ${innerItem.value} `
);
}
return null;
});
return (
<p
style={{ marginBottom: "15px", marginTop: "15px" }}
className={styles.paragraph}
key={index}
>{children}</p>
);
}
if (item.type === "caption") {
return <caption key={index}>{item.value}</caption>;
}
if (item.type === "quote") {
return (
<blockquote cite={item.cite} key={index}>
{item.value}
</blockquote>
);
}
if (item.type === "image") {
return (
<div
key={index}
style={{
display: "flex",
width: "100%",
justifyContent: "center",
}}
>
<Image
alt={item.alt}
src={item.value}
style={{ maxWidth: "100%", width: "550px" }}
/>
</div>
);
}
if (item.type === "code") {
return (
<div
key={index}
style={{
display: "flex",
width: "100%",
maxWidth: "550px",
margin: "auto",
justifyContent: "center",
}}
>
<Editor
height="300px"
width={"100%"}
language={item.lang}
theme={mode == "light" ? "light" : "vs-dark"}
loading={
<span className={styles.loadingEditor}>loading...</span>
}
value={item.value}
options={{
minimap: {
enabled: false,
},
fontSize: 15,
}}
/>
</div>
);
}
if (item.type === "tweet") {
return (
<div
key={index}
className={mode == "light" ? "light" : "dark"}
style={{
display: "flex",
width: "100%",
justifyContent: "center",
}}
>
<Tweet id={item.value} />
</div>
);
}
return null;
})}
</>
);
};
export default renderEngine;
//Content JSON
..."content": [
{
"type": "paragraph",
"value": [
"Lorem ipsum...."
]
},
{
"type": "paragraph",
"value": [
{
"type": "bold",
"value": "Lorem ipsum ....:"
}
]
}
]
![Terminal console.log([paragraph children])](https://i.stack.imgur.com/t4Iyu.png)


