React.js:组件不渲染或根本不显示
React.js: Component not rendering or showing up at all
我正在将一个 bootstrap 模板集成到一个 React 应用程序中,但我在获取两个组件(Home 和 ProductIndex)进行渲染时遇到了问题。当我使用 React 开发工具检查时,组件根本不在 DOM 中。接下来的 html 有一些我还没有开始处理的问题,但它根本不呈现让我很困惑。不确定是什么问题。
这是主页组件
import React, { Component } from 'react'
class Home extends Component {
render() {
return (
<div>
<div className="modal fade" id="productView" tabindex="-1" role="dialog" aria-hidden="true">
<div className="modal-dialog modal-lg modal-dialog-centered" role="document">
<div className="modal-content">
<div className="modal-body p-0">
<div className="row align-items-stretch">
<div className="col-lg-6 p-lg-0"><a className="product-view d-block h-100 bg-cover bg-center" style="background: url(img/product-5.jpg)" href="img/product-5.jpg" data-lightbox="productview" title="Red digital smartwatch"></a><a className="d-none" href="img/product-5-alt-1.jpg" title="Red digital smartwatch" data-lightbox="productview"></a><a className="d-none" href="img/product-5-alt-2.jpg" title="Red digital smartwatch" data-lightbox="productview"></a></div>
<div className="col-lg-6">
<button className="close p-4" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<div className="p-5 my-md-4">
<ul className="list-inline mb-2">
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
</ul>
<h2 className="h4">Red digital smartwatch</h2>
<p className="text-muted">0</p>
<p className="text-small mb-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut ullamcorper leo, eget euismod orci. Cum sociis natoque penatibus et magnis dis parturient montes nascetur ridiculus mus. Vestibulum ultricies aliquam convallis.</p>
<div className="row align-items-stretch mb-4">
<div className="col-sm-7 pr-sm-0">
<div className="border d-flex align-items-center justify-content-between py-1 px-3"><span className="small text-uppercase text-gray mr-4 no-select">Quantity</span>
<div className="quantity">
<button className="dec-btn p-0"><i className="fas fa-caret-left"></i></button>
<input className="form-control border-0 shadow-0 p-0" type="text" value="1"/>
<button className="inc-btn p-0"><i className="fas fa-caret-right"></i></button>
</div>
</div>
</div>
<div className="col-sm-5 pl-sm-0"><a className="btn btn-dark btn-sm btn-block h-100 d-flex align-items-center justify-content-center px-0" href="cart.html">Add to cart</a></div>
</div><a className="btn btn-link text-dark p-0" href="#"><i className="far fa-heart mr-2"></i>Add to wish list</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div className="container">
<section className="hero pb-3 bg-cover bg-center d-flex align-items-center" style="background: url(img/hero-banner-alt.jpg)">
<div className="container py-5">
<div className="row px-4 px-lg-5">
<div className="col-lg-6">
<p className="text-muted small text-uppercase mb-2">New Inspiration 2020</p>
<h1 className="h2 text-uppercase mb-3">20% off on new season</h1><a className="btn btn-dark" href="shop.html">Browse collections</a>
</div>
</div>
</div>
</section>
<section className="pt-5">
<header className="text-center">
<p className="small text-muted small text-uppercase mb-1">Carefully created collections</p>
<h2 className="h5 text-uppercase mb-4">Browse our categories</h2>
</header>
<div className="row">
<div className="col-md-4 mb-4 mb-md-0"><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-1.jpg" alt=""/><strong className="category-item-title">Clothes</strong></a></div>
<div className="col-md-4 mb-4 mb-md-0"><a className="category-item mb-4" href="shop.html"><img className="img-fluid" src="img/cat-img-2.jpg" alt=""/><strong className="category-item-title">Shoes</strong></a><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-3.jpg" alt=""/><strong className="category-item-title">Watches</strong></a></div>
<div className="col-md-4"><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-4.jpg" alt=""/><strong className="category-item-title">Electronics</strong></a></div>
</div>
</section>
<section className="py-5">
<header>
<p className="small text-muted small text-uppercase mb-1">Made the hard way</p>
<h2 className="h5 text-uppercase mb-4">Top trending products</h2>
</header>
<div className="row">
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-1.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Kui Ye Chen’s AirPods</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-primary">Sale</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-2.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Air Jordan 12 gym red</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-3.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Cyan cotton t-shirt</a></h6>
<p className="small text-muted"></p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-info">New</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-4.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Timex Unisex Originals</a></h6>
<p className="small text-muted">1</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-danger">Sold</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-5.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Red digital smartwatch</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-6.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Nike air max 95</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-7.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Joemalone Women prefume</a></h6>
<p className="small text-muted"></p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-8.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Apple Watch</a></h6>
<p className="small text-muted">1</p>
</div>
</div>
</div>
</section>
<section className="py-5 bg-light">
<div className="container">
<div className="row text-center">
<div className="col-lg-4 mb-3 mb-lg-0">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#delivery-time-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">Free shipping</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
<div className="col-lg-4 mb-3 mb-lg-0">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#helpline-24h-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">24 x 7 service</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
<div className="col-lg-4">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#label-tag-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">Festival offer</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section className="py-5">
<div className="container p-0">
<div className="row">
<div className="col-lg-6 mb-3 mb-lg-0">
<h5 className="text-uppercase">Let's be friends!</h5>
<p className="text-small text-muted mb-0">Nisi nisi tempor consequat laboris nisi.</p>
</div>
<div className="col-lg-6">
<form action="#">
<div className="input-group flex-column flex-sm-row mb-3">
<input className="form-control form-control-lg py-3" type="email" placeholder="Enter your email address" aria-describedby="button-addon2"/>
<div className="input-group-append">
<button className="btn btn-dark btn-block" id="button-addon2" type="submit">Subscribe</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
<footer className="bg-dark text-white">
<div className="container py-4">
<div className="row py-5">
<div className="col-md-4 mb-3 mb-md-0">
<h6 className="text-uppercase mb-3">Customer services</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">Help & Contact Us</a></li>
<li><a className="footer-link" href="#">Returns & Refunds</a></li>
<li><a className="footer-link" href="#">Online Stores</a></li>
<li><a className="footer-link" href="#">Terms & Conditions</a></li>
</ul>
</div>
<div className="col-md-4 mb-3 mb-md-0">
<h6 className="text-uppercase mb-3">Company</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">What We Do</a></li>
<li><a className="footer-link" href="#">Available Services</a></li>
<li><a className="footer-link" href="#">Latest Posts</a></li>
<li><a className="footer-link" href="#">FAQs</a></li>
</ul>
</div>
<div className="col-md-4">
<h6 className="text-uppercase mb-3">Social media</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">Twitter</a></li>
<li><a className="footer-link" href="#">Instagram</a></li>
<li><a className="footer-link" href="#">Tumblr</a></li>
<li><a className="footer-link" href="#">Pinterest</a></li>
</ul>
</div>
</div>
<div className="border-top pt-4" style="border-color: #1d1d1d !important">
<div className="row">
<div className="col-lg-6">
<p className="small text-muted mb-0">© 2020 All rights reserved.</p>
</div>
<div className="col-lg-6 text-lg-right">
<p className="small text-muted mb-0">Template designed by <a className="text-white reset-anchor" href="https://bootstraptemple.com/p/bootstrap-ecommerce">Bootstrap Temple</a></p>
</div>
</div>
</div>
</div>
</footer>
</div>
)
}
}
export default Home
这里是Header组件(这是渲染)
import React, { Component } from 'react';
class Header extends Component {
render() {
return (
<header class="header bg-white">
<div class="container px-0 px-lg-3">
<nav class="navbar navbar-expand-lg navbar-light py-3 px-lg-0"><a class="navbar-brand" href="index.html"><span class="font-weight-bold text-uppercase text-dark">Boutique</span></a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link active" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="shop.html">Shop</a>
</li>
<li class="nav-item">
<a class="nav-link" href="detail.html">Product detail</a>
</li>
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" id="pagesDropdown" href="#nav" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Pages</a>
<div class="dropdown-menu mt-3" aria-labelledby="pagesDropdown"><a class="dropdown-item border-0 transition-link" href="index.html">Homepage</a><a class="dropdown-item border-0 transition-link" href="shop.html">Category</a><a class="dropdown-item border-0 transition-link" href="detail.html">Product detail</a><a class="dropdown-item border-0 transition-link" href="cart.html">Shopping cart</a><a class="dropdown-item border-0 transition-link" href="checkout.html">Checkout</a></div>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="cart.html"> <i class="fas fa-dolly-flatbed mr-1 text-gray"></i>Cart<small class="text-gray">(2)</small></a></li>
<li class="nav-item"><a class="nav-link" href="#nav"> <i class="far fa-heart mr-1"></i><small class="text-gray"> (0)</small></a></li>
<li class="nav-item"><a class="nav-link" href="#nav"> <i class="fas fa-user-alt mr-1 text-gray"></i>Login</a></li>
</ul>
</div>
</nav>
</div>
</header>
)
}
}
export default Header;
这里是App.js
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home/Home.js'
import Header from './Header/Header.js'
import ProductIndex from './ProductIndex/ProductIndex.js'
function App() {
return (
<div className="wrapper">
<Router>
<Switch>
<Header/>
<Route exact path="/" component={ Home } />
<Route exact path="/products" component={ ProductIndex } />
</Switch>
</Router>
</div>
);
}
export default App;
和index.js
import React from 'react'
import ReactDOM from 'react-dom'
// import './index.scss'
import App from './App'
import { BrowserRouter } from 'react-router-dom'
const appJsx = (
<BrowserRouter>
<App />
</BrowserRouter>
)
ReactDOM.render(appJsx, document.getElementById('root'))
组件没有出现的原因是因为 Header 在 Switch 里面,它应该在外面
function App() {
return (
<div className="wrapper">
<Header/>
<Router>
<Switch>
<Route exact path="/" component={ Home } />
<Route exact path="/products" component={ ProductIndex } />
</Switch>
</Router>
</div>
);
}
此外,您的代码中有很多问题,请查看控制台...
我正在将一个 bootstrap 模板集成到一个 React 应用程序中,但我在获取两个组件(Home 和 ProductIndex)进行渲染时遇到了问题。当我使用 React 开发工具检查时,组件根本不在 DOM 中。接下来的 html 有一些我还没有开始处理的问题,但它根本不呈现让我很困惑。不确定是什么问题。
这是主页组件
import React, { Component } from 'react'
class Home extends Component {
render() {
return (
<div>
<div className="modal fade" id="productView" tabindex="-1" role="dialog" aria-hidden="true">
<div className="modal-dialog modal-lg modal-dialog-centered" role="document">
<div className="modal-content">
<div className="modal-body p-0">
<div className="row align-items-stretch">
<div className="col-lg-6 p-lg-0"><a className="product-view d-block h-100 bg-cover bg-center" style="background: url(img/product-5.jpg)" href="img/product-5.jpg" data-lightbox="productview" title="Red digital smartwatch"></a><a className="d-none" href="img/product-5-alt-1.jpg" title="Red digital smartwatch" data-lightbox="productview"></a><a className="d-none" href="img/product-5-alt-2.jpg" title="Red digital smartwatch" data-lightbox="productview"></a></div>
<div className="col-lg-6">
<button className="close p-4" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<div className="p-5 my-md-4">
<ul className="list-inline mb-2">
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
<li className="list-inline-item m-0"><i className="fas fa-star small text-warning"></i></li>
</ul>
<h2 className="h4">Red digital smartwatch</h2>
<p className="text-muted">0</p>
<p className="text-small mb-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut ullamcorper leo, eget euismod orci. Cum sociis natoque penatibus et magnis dis parturient montes nascetur ridiculus mus. Vestibulum ultricies aliquam convallis.</p>
<div className="row align-items-stretch mb-4">
<div className="col-sm-7 pr-sm-0">
<div className="border d-flex align-items-center justify-content-between py-1 px-3"><span className="small text-uppercase text-gray mr-4 no-select">Quantity</span>
<div className="quantity">
<button className="dec-btn p-0"><i className="fas fa-caret-left"></i></button>
<input className="form-control border-0 shadow-0 p-0" type="text" value="1"/>
<button className="inc-btn p-0"><i className="fas fa-caret-right"></i></button>
</div>
</div>
</div>
<div className="col-sm-5 pl-sm-0"><a className="btn btn-dark btn-sm btn-block h-100 d-flex align-items-center justify-content-center px-0" href="cart.html">Add to cart</a></div>
</div><a className="btn btn-link text-dark p-0" href="#"><i className="far fa-heart mr-2"></i>Add to wish list</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div className="container">
<section className="hero pb-3 bg-cover bg-center d-flex align-items-center" style="background: url(img/hero-banner-alt.jpg)">
<div className="container py-5">
<div className="row px-4 px-lg-5">
<div className="col-lg-6">
<p className="text-muted small text-uppercase mb-2">New Inspiration 2020</p>
<h1 className="h2 text-uppercase mb-3">20% off on new season</h1><a className="btn btn-dark" href="shop.html">Browse collections</a>
</div>
</div>
</div>
</section>
<section className="pt-5">
<header className="text-center">
<p className="small text-muted small text-uppercase mb-1">Carefully created collections</p>
<h2 className="h5 text-uppercase mb-4">Browse our categories</h2>
</header>
<div className="row">
<div className="col-md-4 mb-4 mb-md-0"><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-1.jpg" alt=""/><strong className="category-item-title">Clothes</strong></a></div>
<div className="col-md-4 mb-4 mb-md-0"><a className="category-item mb-4" href="shop.html"><img className="img-fluid" src="img/cat-img-2.jpg" alt=""/><strong className="category-item-title">Shoes</strong></a><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-3.jpg" alt=""/><strong className="category-item-title">Watches</strong></a></div>
<div className="col-md-4"><a className="category-item" href="shop.html"><img className="img-fluid" src="img/cat-img-4.jpg" alt=""/><strong className="category-item-title">Electronics</strong></a></div>
</div>
</section>
<section className="py-5">
<header>
<p className="small text-muted small text-uppercase mb-1">Made the hard way</p>
<h2 className="h5 text-uppercase mb-4">Top trending products</h2>
</header>
<div className="row">
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-1.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Kui Ye Chen’s AirPods</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-primary">Sale</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-2.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Air Jordan 12 gym red</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-3.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Cyan cotton t-shirt</a></h6>
<p className="small text-muted"></p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-info">New</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-4.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Timex Unisex Originals</a></h6>
<p className="small text-muted">1</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-danger">Sold</div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-5.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Red digital smartwatch</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-6.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Nike air max 95</a></h6>
<p className="small text-muted">0</p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-7.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Joemalone Women prefume</a></h6>
<p className="small text-muted"></p>
</div>
</div>
<div className="col-xl-3 col-lg-4 col-sm-6">
<div className="product text-center">
<div className="position-relative mb-3">
<div className="badge text-white badge-"></div><a className="d-block" href="detail.html"><img className="img-fluid w-100" src="img/product-8.jpg" alt="..."/></a>
<div className="product-overlay">
<ul className="mb-0 list-inline">
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-outline-dark" href="#"><i className="far fa-heart"></i></a></li>
<li className="list-inline-item m-0 p-0"><a className="btn btn-sm btn-dark" href="cart.html">Add to cart</a></li>
<li className="list-inline-item mr-0"><a className="btn btn-sm btn-outline-dark" href="#productView" data-toggle="modal"><i className="fas fa-expand"></i></a></li>
</ul>
</div>
</div>
<h6> <a className="reset-anchor" href="detail.html">Apple Watch</a></h6>
<p className="small text-muted">1</p>
</div>
</div>
</div>
</section>
<section className="py-5 bg-light">
<div className="container">
<div className="row text-center">
<div className="col-lg-4 mb-3 mb-lg-0">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#delivery-time-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">Free shipping</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
<div className="col-lg-4 mb-3 mb-lg-0">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#helpline-24h-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">24 x 7 service</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
<div className="col-lg-4">
<div className="d-inline-block">
<div className="media align-items-end">
<svg className="svg-icon svg-icon-big svg-icon-light">
<use href="#label-tag-1"> </use>
</svg>
<div className="media-body text-left ml-3">
<h6 className="text-uppercase mb-1">Festival offer</h6>
<p className="text-small mb-0 text-muted">Free shipping worlwide</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section className="py-5">
<div className="container p-0">
<div className="row">
<div className="col-lg-6 mb-3 mb-lg-0">
<h5 className="text-uppercase">Let's be friends!</h5>
<p className="text-small text-muted mb-0">Nisi nisi tempor consequat laboris nisi.</p>
</div>
<div className="col-lg-6">
<form action="#">
<div className="input-group flex-column flex-sm-row mb-3">
<input className="form-control form-control-lg py-3" type="email" placeholder="Enter your email address" aria-describedby="button-addon2"/>
<div className="input-group-append">
<button className="btn btn-dark btn-block" id="button-addon2" type="submit">Subscribe</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
<footer className="bg-dark text-white">
<div className="container py-4">
<div className="row py-5">
<div className="col-md-4 mb-3 mb-md-0">
<h6 className="text-uppercase mb-3">Customer services</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">Help & Contact Us</a></li>
<li><a className="footer-link" href="#">Returns & Refunds</a></li>
<li><a className="footer-link" href="#">Online Stores</a></li>
<li><a className="footer-link" href="#">Terms & Conditions</a></li>
</ul>
</div>
<div className="col-md-4 mb-3 mb-md-0">
<h6 className="text-uppercase mb-3">Company</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">What We Do</a></li>
<li><a className="footer-link" href="#">Available Services</a></li>
<li><a className="footer-link" href="#">Latest Posts</a></li>
<li><a className="footer-link" href="#">FAQs</a></li>
</ul>
</div>
<div className="col-md-4">
<h6 className="text-uppercase mb-3">Social media</h6>
<ul className="list-unstyled mb-0">
<li><a className="footer-link" href="#">Twitter</a></li>
<li><a className="footer-link" href="#">Instagram</a></li>
<li><a className="footer-link" href="#">Tumblr</a></li>
<li><a className="footer-link" href="#">Pinterest</a></li>
</ul>
</div>
</div>
<div className="border-top pt-4" style="border-color: #1d1d1d !important">
<div className="row">
<div className="col-lg-6">
<p className="small text-muted mb-0">© 2020 All rights reserved.</p>
</div>
<div className="col-lg-6 text-lg-right">
<p className="small text-muted mb-0">Template designed by <a className="text-white reset-anchor" href="https://bootstraptemple.com/p/bootstrap-ecommerce">Bootstrap Temple</a></p>
</div>
</div>
</div>
</div>
</footer>
</div>
)
}
}
export default Home
这里是Header组件(这是渲染)
import React, { Component } from 'react';
class Header extends Component {
render() {
return (
<header class="header bg-white">
<div class="container px-0 px-lg-3">
<nav class="navbar navbar-expand-lg navbar-light py-3 px-lg-0"><a class="navbar-brand" href="index.html"><span class="font-weight-bold text-uppercase text-dark">Boutique</span></a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link active" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="shop.html">Shop</a>
</li>
<li class="nav-item">
<a class="nav-link" href="detail.html">Product detail</a>
</li>
<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" id="pagesDropdown" href="#nav" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Pages</a>
<div class="dropdown-menu mt-3" aria-labelledby="pagesDropdown"><a class="dropdown-item border-0 transition-link" href="index.html">Homepage</a><a class="dropdown-item border-0 transition-link" href="shop.html">Category</a><a class="dropdown-item border-0 transition-link" href="detail.html">Product detail</a><a class="dropdown-item border-0 transition-link" href="cart.html">Shopping cart</a><a class="dropdown-item border-0 transition-link" href="checkout.html">Checkout</a></div>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="cart.html"> <i class="fas fa-dolly-flatbed mr-1 text-gray"></i>Cart<small class="text-gray">(2)</small></a></li>
<li class="nav-item"><a class="nav-link" href="#nav"> <i class="far fa-heart mr-1"></i><small class="text-gray"> (0)</small></a></li>
<li class="nav-item"><a class="nav-link" href="#nav"> <i class="fas fa-user-alt mr-1 text-gray"></i>Login</a></li>
</ul>
</div>
</nav>
</div>
</header>
)
}
}
export default Header;
这里是App.js
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home/Home.js'
import Header from './Header/Header.js'
import ProductIndex from './ProductIndex/ProductIndex.js'
function App() {
return (
<div className="wrapper">
<Router>
<Switch>
<Header/>
<Route exact path="/" component={ Home } />
<Route exact path="/products" component={ ProductIndex } />
</Switch>
</Router>
</div>
);
}
export default App;
和index.js
import React from 'react'
import ReactDOM from 'react-dom'
// import './index.scss'
import App from './App'
import { BrowserRouter } from 'react-router-dom'
const appJsx = (
<BrowserRouter>
<App />
</BrowserRouter>
)
ReactDOM.render(appJsx, document.getElementById('root'))
组件没有出现的原因是因为 Header 在 Switch 里面,它应该在外面
function App() {
return (
<div className="wrapper">
<Header/>
<Router>
<Switch>
<Route exact path="/" component={ Home } />
<Route exact path="/products" component={ ProductIndex } />
</Switch>
</Router>
</div>
);
}
此外,您的代码中有很多问题,请查看控制台...