Hi Everyone I am Stuck in a problem since monday and a can’t seem to solve the issue.
I am trying to implement a rich text editor in my application which is fullstack with node and Mongo atlas.
The rich text editor that I am using is slate.js.
the editor is done and no issues there I have set the state and the onChange function in a separate file using Context api
{
/** Editor state */
}
const [content, setContent] = useState([
{
type: "paragraph",
children: [{ text: "Write your snippets here" }],
},
]);
/** handleChange Editor */
const handleChangeEditor = (content) => {
setContent(content);
};
/** onSubmit Content */
const onSubmit = async (e) => {
e.preventDefault();
let variables = {
content,
tech,
};
const snippetPost = await Axios.post(
"http://localhost:5000/snippets/",
variables,
{
headers: {
"x-auth-token":"token here"
},
}
);
setContent([
{
type: "paragraph",
children: [{ text: "Write your snippets here" }],
},
]);
my issues is that I am not able to display to content on the page, I tried to serialize the content coming from Mongo but the nesting array and object it makes the serialize function not working, I am trying to get deeper to the object to serialize but nothing no results.
const SnippetsContainer = () => {
const [snippetsContent, setSnippetsContent] = useState([]);
const serialize = (node) => {
if (Text.isText(node)) {
return escapeHtml(node.text);
}
const children = node.children.map((n) => serialize(n)).join("");
switch (node.type) {
case "quote":
return `<blockquote><p>${children}</p></blockquote>`;
case "paragraph":
return `<p>${children}</p>`;
case "link":
return `<a href="${escapeHtml(node.url)}">${children}</a>`;
default:
return children;
}
};
const getSnippets = async () => {
try {
const res = await Axios.get("http://localhost:5000/snippets/all", {
headers: {
"x-auth-token":"token here"
},
});
const content = res.data;
return setSnippetsContent(content);
} catch (err) {
console.log(err);
}
};
useEffect(() => {
getSnippets();
}, []);
{
/*
const result = snippetsContent.map((snippet) => {
let firstResult = snippet.content;
console.log(firstResult);
});
*/
}
return <div></div>;
};
I also tried dangerouslysetInnerhtml but if I can’t serialize to nested object I have nothing to pass to the _html key.
I am using the primary version of slate
Thanks for your help