简体   繁体   English

在 React 中渲染 Bootstrap 组件时出现空白页

[英]Blank page when rendering Bootstrap Component in React

I am kind of new at Front-end development, and I encountered the following bug.我是前端开发的新手,遇到了以下错误。
So, I was designing my own project - local store site, every component created by me, but eventually I came to the moment where I would like to use Carousel for displaying products etc, since I'm new and I just wanted to use clean Carousel designs I decided to use Boostrap for React (React-Boostrap).所以,我正在设计我自己的项目 - 本地商店网站,我创建的每个组件,但最终我来到了我想使用Carousel来展示产品等的那一刻,因为我是新手,我只想使用 clean轮播设计我决定使用 Boostrap for React (React-Boostrap)。 However, when I use it in my component, it renders my page completely blank, it even removes my Navbar component at the top.但是,当我在我的组件中使用它时,它会使我的页面完全空白,它甚至会删除顶部的导航栏组件。

I guess it is some Route issue which I could not identify by myself, so please kind developers, I need some help.我想这是我自己无法识别的一些路线问题,所以请善良的开发人员,我需要一些帮助。

Here is the code:这是代码:
The Carousel component, just copied and pasted it , and wrapped it in Container (yes I tried to remove the container too that's not the issue), Carousel 组件,只是复制并粘贴它,并将其包装在 Container 中(是的,我也尝试删除容器,这不是问题),

import React from 'react'
import { MainContainer } from '../styles/Products.style'
import Carousel from 'react-bootstrap/Carousel'

const Products = () => {
  return (
    <MainContainer>
      <Carousel>
    <Carousel.Item>
      <img
        className="d-block w-100"
        src="holder.js/800x400?text=First slide&bg=373940"
        alt="First slide"
      />
      <Carousel.Caption>
        <h3>First slide label</h3>
        <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
      </Carousel.Caption>
    </Carousel.Item>
    <Carousel.Item>
      <img
        className="d-block w-100"
        src="holder.js/800x400?text=Second slide&bg=282c34"
        alt="Second slide"
      />
  
      <Carousel.Caption>
        <h3>Second slide label</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </Carousel.Caption>
    </Carousel.Item>
    <Carousel.Item>
      <img
        className="d-block w-100"
        src="holder.js/800x400?text=Third slide&bg=20232a"
        alt="Third slide"
      />
  
      <Carousel.Caption>
        <h3>Third slide label</h3>
        <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
      </Carousel.Caption>
    </Carousel.Item>
  </Carousel> 
  </MainContainer>
     
  )
}

export default Products

This is the App.js :这是 App.js :

import "./App.css";
import Navbar from "./components/Navbar";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Login from "./components/Login";
import Home from "./components/Home";
import Products from "./components/Products";



function App() {
  return (
    <Router>
      <Navbar />
      
      <Routes>
        <Route path="/" element={<Home/>}/>
        <Route path="/products" element={<Products/>}/>
        <Route path="/contact" />
        <Route path="/about" />
        <Route path="/signup" element={<Login/>} />
        <Route path="/logout" />
        
      </Routes>

    </Router>
  );
}

export default App;

Annd Im not sure if it helps but, pasting it just in case - package.json我不确定它是否有帮助,但粘贴它以防万一 - package.json

{
  "dependencies": {
    "axios": "^0.27.2",
    "bootstrap": "^5.1.3",
    "react-bootstrap": "^2.4.0",
    "react-reveal": "^1.2.2",
    "react-router-dom": "^6.3.0" // added this manually, just to try it it didnt work
  }
}

I have copy/pasted your code and when I go to "/products" I can see the slider.我已经复制/粘贴了您的代码,当我转到“/products”时,我可以看到滑块。 However I had to comment out MainContainer because I have no code for it.但是我不得不注释掉 MainContainer 因为我没有它的代码。 (But you have said MainContainer is not the issue anyway). (但是您已经说过 MainContainer 无论如何都不是问题)。

https://codesandbox.io/s/elastic-vaughan-6bjv3t https://codesandbox.io/s/elastic-vaughan-6bjv3t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM