所以我正在完成这个任务,我将原始 html 转换为一个反应组件,我需要完成这个任务 4 次,导航栏中的 4 个菜单。代码有效,但是代码看起来很难看,我相信有一种方法可以用更少的行完成任务。我希望更有效的重复代码是 processinginstructions 和 processinginstructionsTwo。代码在这里:
import React, { useState, useEffect } from "react";
import { Grid, Container } from "@material-ui/core";
import { navSubMenuStyles } from "./Styles";
import "./stylesheets/NavBar.css";
import NavBar from "./NavBar";
import NavMenuLink from "./stylesheets/NavMenuLink";
function NavBarContent() {
const pageName = ["HIDDEN-FOR-PRIVACY", "HIDDEN-FOR-PRIVACY"];
const [content, setContent] = useState();
const [contenttwo, setContenttwo] = useState();
async function fetchData() {
const res = await fetch("HIDDEN-FOR-PRIVACY" + pageName);
res
.json()
.then((res) => {
setContent(res[0].content.rendered.replace(/(\r\n|\n|\r)/gm, ""));
setContenttwo(res[1].content.rendered.replace(/(\r\n|\n|\r)/gm, ""));
})
.catch((err) => console.log(err));
}
useEffect(() => {
fetchData();
}, []);
var HtmlToReact = require("html-to-react");
var HtmlToReactParser = require("html-to-react").Parser;
var htmlInput = content;
var htmlInputTwo = contenttwo;
// Order matters. Instructions are processed in the order they're defined
var processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React);
var processingInstructions = [
{
//// subMenus \\// subMenus \\\\
replaceChildren: true,
shouldProcessNode: function (node) {
return node.attribs && node.attribs["data-cat"] === "submenu";
},
processNode: function (node, children) {
return (
<li className="menu-link">
{pageName[0]}
<NavBar children={children} />
</li>
);
},
},
{
// replaceChildren:true,
shouldProcessNode: function (node) {
return node.parent && node.parent.name && node.parent.name === "h1";
},
processNode: function (node, children) {
return null;
},
},
{
// Anything else
shouldProcessNode: function (node) {
return true;
},
processNode: processNodeDefinitions.processDefaultNode,
},
];
var processingInstructionsTwo = [
{
//// subMenus \\// subMenus \\\\
replaceChildren: true,
shouldProcessNode: function (node) {
return node.attribs && node.attribs["data-cat"] === "submenu";
},
processNode: function (node, children) {
return (
<li className="menu-link">
{pageName[1]}
<NavBar children={children} />
</li>
);
},
},
{
// replaceChildren:true,
shouldProcessNode: function (node) {
return node.parent && node.parent.name && node.parent.name === "h1";
},
processNode: function (node, children) {
return null;
},
},
{
// Anything else
shouldProcessNode: function (node) {
return true;
},
processNode: processNodeDefinitions.processDefaultNode,
},
];
var htmlToReactParser = new HtmlToReactParser();
var reactComponent = htmlToReactParser.parseWithInstructions(
htmlInput,
() => true,
processingInstructions
);
var reactComponenttwo = htmlToReactParser.parseWithInstructions(
htmlInputTwo,
() => true,
processingInstructionsTwo
);
return (
<div style={{ backgroundColor: "silver" }}>
<Container
style={{
maxWidth: "1060px",
width: "100%",
height: "auto",
display: "block",
}}
>
<Grid container alignItems="center" style={{ display: "flex" }}>
<Grid item xs={5}>
Logo
</Grid>
<Grid item xs={7}>
<nav>
<ul style={{ display: "flex" }}>
{reactComponent}
{reactComponenttwo}
</ul>
</nav>
</Grid>
</Grid>
</Container>
</div>
);
}
export default NavBarContent;
任何帮助将非常感激。