React bootstrap layout issue - javascript

I was wrapping my head around the layout for React-bootstrap. Tried the basic layout using Container, Row and Col. You can access the sandbox here.
What I understood from the react-bootstrap docs is the columns should have been put horizontally rather being stacked on top of each other. What I am trying to achieve is get those columns stacked horitzontally.

You have to import the bootstrap CSS file, bootstrap/dist/css/bootstrap.min.css. Try the following:
import "./styles.css";
import { Container, Row, Col } from "react-bootstrap";
import 'bootstrap/dist/css/bootstrap.min.css';
export default function App() {
return (
<div className="App">
<Container>
<Row>
<Col>Col 1 of 4</Col>
<Col>Col 2 of 4</Col>
<Col>Col 3 of 4</Col>
<Col>Col 4 of 4</Col>
</Row>
</Container>
</div>
);
}
References:
StackOverflow. Container, Row, Col - in React-Bootstrap not working. https://stackoverflow.com/a/60673785/8121551. (Accessed 22 August, 2021).

Related

Capitalized Component Not Rendering in React

I am importing a component, but it does not render. The component is capitalized. I think there is something wrong with the import statement because when I remove it and replace with "Navbar", it renders, but if i keep the import then it does not render.
import styles from "./style";
import { Billing, Business, CardDeal, Clients, CTA, Footer, Navbar, Stats, Testimonials, Hero }
from "./components";
const App = () => { return (
<div className="bg-primary w-full overflow-hidden">
<div className={`${styles.paddingX} ${styles.flexCenter}`}>
<div className={`${styles.boxWidth}`}>
<Navbar />
</div>
</div>
</div>
);
};
export default App;
import React from 'react';
const Navbar = () => {
return (
<div>Navbar</div>
);
};
export default Navbar;
import Navbar from "./Navbar";
import Billing from "./Billing";
import CardDeal from "./CardDeal";
import Business from "./Business";
import Clients from "./Clients";
import CTA from "./CTA";
import Stats from "./Stats";
import Footer from "./Footer";
import Testimonials from "./Testimonials";
import Hero from "./Hero";
export {
Navbar,
Billing,
CardDeal,
Business,
Clients,
CTA,
Stats,
Footer,
Testimonials,
Hero,
};
If i exchange the component
for basic text like "Navbar" or "Navbar" it does not render either. These 2 display when I remove the import components statement.
When i control click , it brings me to the correct Navbar component.
I am following a yt tutorial
https://github.com/adrianhajdin/project_hoobank
my repo is here: https://github.com/lukasmckaine22/userbet_landing_page
I made sure the component was capitalized. I confirmed the component actually leads to the correct component by control clicking it. I confirmed the code renders plain text only when the import is removed. I confirmed the import leads to the correct file by control clicking. I tried only importing the Navbar with and without brackets. I changed the import directory to pull the navbar directory.

React - how to divide components

I'm trying to use the header and sidebar from React Bootstrap and semantic UI react.
But when I add those components to my page they overlap each other.
How can I divide them? The sidebar, header, and main content should be separated.
Is there a way to create layouts in react?
App.js
import { Container, Row, Col } from "react-bootstrap";
import Stack from "react-bootstrap/Stack";
import Cardtest from "./Components/Card";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import SidebarTest from "./Components/Sidebar";
import Header from "./Components/Header";
function App() {
return (
<div>
<div>
<Header />
</div>
<SidebarTest />
<main>
Main content
</main>
</div>
);
}
export default App;
Its hard to tell without the CSS but try changing the nav bar position from
position:absolute;

How can I fix the error on my React App after trying to export a functional component?

My biggest problem is that I have not been able to get my react app to display since the first week of the course's assignment. I am at the end of the second week, and have been dealing with this second assignment for several weeks since the course is online through coursera.org.
I have gone over the assignment from week one and watched the videos over and over again, and made many revisions after googling a lot of the answers to the correct code. I have also gone through the resources of week one that the course offers.
I posted numerous times on week one's discussion board and follow the suggestions given. I still was not able to get the react app to display, so I moved one the week 2 assignment, and continued working on it.
After more than two weeks of a lot of trial and error and following the error prompts from the visual studio which is the one I am using for this course, I finally started to get some input on what errors I needed to fix on the editor through the cmd window. Every time I make corrections and run yarn start again, the app started to let me know what I needed to fix on the code. The following image is the last error that I got. Any suggestions are greatly appreciated.
This is the code of the functional component that is giving me the error.
import React from 'react';
import { Card, CardImg, CardImgOverlay, CardTitle, Breadcrumb, BreadcrumbItem } from 'reactstrap';
import {Link } from 'react-router-dom';
import { DishDetail } from 'reactstrap';
import { Menu } from 'reactstrap';
function RenderMenuItem ({dish, onClick}) {
return(
<card>
<Link to={`/menu/${dish.id}`} >
<cardImg width = "100%" src ={dish.image} alt={dish.name} />
<cardImgOverlay>
<cardTitle>
{dish.name}
</cardTitle>
</cardImgOverlay>
</Link>
</card>
);
}
function Millie (props) {
const menu = props.dishes.map((dish)=>{
return(
<div key={dish.id} className="col-12 col-md-5 m-1">
<RenderMenuItem dish={dish} />
</div>
);
});
}
return(
<div className="container">
<div className="row">
<Breadcrumb>
<BreadcrumbItem><Link to="/home">Home</Link></BreadcrumbItem>
<BreadcrumbItem active>Menu</BreadcrumbItem>
</Breadcrumb>
<div className="col-12">
<h3>Menu</h3>
<hr />
</div>
</div>
<div className="row">
{menu}
</div>
</div>
);
export default function Menu(){
}
Almost all if code has been provided by professor.
Error page from React App
Any suggestions are greatly appreciated!
ty
Mildred
As the error says, you're trying to declare something called "Menu" when something called "Menu" already exists.
import { Menu } from 'reactstrap';
export default function Menu(){
}
Either rename the Menu function component or rename the imported one:
// either rename this on import
import { Menu as ReactStrapMenu } from 'reactstrap';
// or call your Menu component something else
export default function MyMenu () {
}
As Chris points out in the comment below, it seems likely that you don't intend to export the empty Menu function at the bottom at all, and perhaps meant to export your Millie component? In that case, just replace the default export at the end with:
export default Millie;
Or export it in the same place you're defining it:
export default function Millie (props) {
const menu = props.dishes.map((dish)=>{
// etc.
}
Mildred! Nice that you're practicing, courses are awesome to learn the basics.
As described in the error you provided, "Menu" is declared twice. If you check the code snippet, you can see that you imported "Menu" from a library, but also declared a function called "Menu".
That is where that error comes from. Just change your default function to any other name, such as "MyMenu"

Styling custom component in GatsbyJS using styled-components

I recently started using Gatsby for building my websites, previously I relied just on plain html and css, so I may be missing something really basic here...
I am trying to style a custom header component that looks like this
import React from "react"
import MWidth from "./m-width"
import logo from "../resources/images/logo.png"
function Header() {
return (
<>
<MWidth>
<div>
<img src={`${logo}`}></img>
</div>
</MWidth>
</>
)
}
export default Header
after importing it inside the layout component I tried styling it with styled-components like so
const PageHeader = styled(Header)`
background-color: #f0f;
`
but nothing changed.
I saw this approach being used with the Link component, but maybe it's defined in another way. Am I missing something or is it just a Gatsby error?
My Layout.js file looks like this
import React from "react"
import styled from "styled-components"
import Header from "./header"
import Content from "./content"
import Footer from "./footer"
import "./common.css"
const PageHeader = styled(Header)`
background-color: #f0f;
`
function Layout(props) {
return (
<>
<PageHeader />
<Content>{props.children}</Content>
<Footer />
</>
)
}
export default Layout
Let me know if you need more information. Any help would be appreciated.
Thanks 😉
Edit:
Turns out that in order for this to work you have to attach a class name to the element you want to style passing it as a prop.So as ksav suggested I added props into the Header function declaration and className={props.className} to a wrapper div. Now it looks like this
function Header(props) {
return (
<div className={props.className}>
<MWidth>
<div>
<img src={`${logo}`}></img>
</div>
</MWidth>
</div>
)
}
which essentially is the same thing as the one he posted below. And this solved the problem.
Thank you 😄
Styling any component
The styled method works perfectly on all of your own or any third-party component, as long as they attach the passed className prop to a DOM element.
function Header({className}) {
return (
<div className={className}>
<MWidth>
<div>
<img src={`${logo}`}></img>
</div>
</MWidth>
</div>
)
}

"Value is declared but never read" but i am declaring the value

I'm trying to build a portfolio website in React ,i'm very new to React (about 3 days), and i have imported some code for the website nav bar and declared it in the code but it doesn't show up on the website and vscode says that the value was not declared. How do i fix this?
I've tried turning the code into a component and rearranging the code but i still get the same result.
App.js - main code
import React from 'react';
import './App.css';
import navBar from './navBar/navBar';
function App() {
return (
<div className="app">
//navBar that won't show up
<navBar />
<div className="landingPage"></div>
<div className="projectPage"></div>
<div className="aboutPage"></div>
<div className="footer"></div>
</div>
)
}
export default App;
Code for the navBar
import React from "react";
import "./navBar.css";
function navBar() {
return (
<div>
<h1>NAVBAR PLACEHOLDER</h1>
</div>
)
}
export default navBar;
Your question title is little bit wierd. You have fixed the value error in your code. But you still have error with the component:
//navBar that won't show up
<navBar />
In react custom built components name should always start with capital letter. So do this:
import NavBar from './navBar/navBar';
// -- ^^ you can name it anything as it's default import
<NavBar />

Categories

Resources