因此,我尝试设置一个为React创建面包屑的组件。
代码在这里:
https://codesandbox.io/s/dreamy-microservice-mjxg2?fontsize=14
它带回来的错误是我对props.children.map的处理不当,可能有人告诉我原因或解决方法吗?
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const Breadcrumb = ({props}) => {
const list = props.children.map((item) => (<li>{item}</li>));
return (
<ul>{list}</ul>
}:
const BreadcrumbItem = ({href, name}) => <div>{name}</div>;
function App() {
return (
<div className="App">
<Breadcrumb>
<BreadcrumbItem
href="/dashboard"
name="Dashboard"
/>
<BreadcrumbItem
href="/brand"
name="Brands"
/>
<Breadcrumb
href="/brand/new"
name="New brands"
/>
</Breadcrumb>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 0 :(得分:2)
更改此:
const Breadcrumb = ({props}) =>
使用
const Breadcrumb = (props) =>
此外,App组件中有一个错字。最后一个孩子是Breadcrumb
,应该是BreadcrumbItem
。
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const Breadcrumb = (props ) => {
const list = props.children.map(item => <li>{item}</li>);
return <ul>{list}</ul>;
};
const BreadcrumbItem = ({ href, name }) => <div>{name}</div>;
function App() {
return (
<div className="App">
<Breadcrumb>
<BreadcrumbItem href="/dashboard" name="Dashboard" />
<BreadcrumbItem href="/brand" name="Brands" />
<BreadcrumbItem href="/brand/new" name="New brands" />
</Breadcrumb>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);