I am having trouble with my React web app as it cannot find my images folder. I have a Navbar component inside a nav folder inside my component folder. Just outside the component folder is the assets folder which houses my img folder.
Like so:
src/
assets/
img/
logo.png
components/
nav/
Navbar.js
Failed to compile.
./src/components/nav/Navbar.js
Module not found: Can't resolve './assets/img/logo.png' in 'C:\xampp\htdocs\MXlionsfieldReact\mxlionsfieldreact\src\components\nav'
Here is my Navbar.js component:
import React, { Component } from 'react';
import Logo from './assets/img/logo.png';
class CustomNavbar extends Component {
render() {
return(
<nav className="navbar navbar-expand-md navbar-dark transparent-nav-custom fixed-top" id="navbar"><a className="navbar-brand" href="/"><img className="pb-2 logo-image" src={Logo} alt="Lionsfield brand Logo" /></a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation"><span className="navbar-toggler-icon"></span></button>
<div className="collapse navbar-collapse" id="navbarsExampleDefault">
<ul className="navbar-nav ml-auto">
<li className="nav-item"><a className="text-center nav-link" href="/"><i className="fa fa-home"></i><br />Inicio <span className="sr-only">(current)</span></a></li>
<li className="nav-item"><a className="text-center nav-link" href="/Quienes-Somos"> <i className="fa fa-users"></i><br />Quienes Somos</a></li>
<li className="nav-item"><a className="text-center nav-link" href="/NuestrosClientes"> <i className="fa fa-building"></i><br />Nuestros Clientes</a></li>
<li className="nav-item"><a className="text-center nav-link" href="/Metodologia"><i className="fa fa-line-chart"></i><br />Metodología</a></li>
<li className="nav-item"><a className="text-center nav-link" href="/Certificaciones"><i className="fa fa-graduation-cap"></i><br />Certificaciones</a></li>
<li className="nav-item"><a className="text-center nav-link" href="/Contacto"> <i className="fa fa-envelope"></i><br />Contacto</a></li>
</ul>
</div>
</nav>
)
}
}
export default CustomNavbar
I am running Node version 10.15.1, NPM version 6.9.0.
This is what you're looking for.
import Logo from '../../assets/img/logo.png';
Your import is expecting assets to be under the nav directory.
Create-React-App's webpack config is pretty flexible, and will allow
import Logo from 'assets/img/logo.png';
or
import Logo from '../../assets/img/logo.png';
Although if you were to move your components folder in the future, it'd break this import.
Check out the Create-React-App documentation for absolute imports here:
https://facebook.github.io/create-react-app/docs/importing-a-component#absolute-imports
Use ../../assets/img/logo.png as the relative path of your logo. . represents the current directory while .. represents the parent directory.
Related
I have this bootstrap NavBar that works perfectly with "a", but when I try to replace the "a" with "Link" from React it pretty much breaks the app, the Navbar stops showing. Is it because of the Bootstrap? I saw a video from youtube that this works, maybe with newer versions this doesn't work?
import React from "react";
import { Link } from "react-router-dom";
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<div className="container-fluid">
<div>
<img width="100" src="/assets/images/logo.jpg"></img>
</div>
<button
className="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
<li className="nav-item">
<Link className="nav-link" to="/">Home</Link> <----------------here for ex.
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Productos
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Nosotros
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Contacto
</a>
</li>
</ul>
</div>
</div>
</nav>
Judging from the href attribute in your <a> components, I believe you are trying to access a component on the same page using hashes.
Link from react-router-dom does not actually support this feature from what I know. Instead, I suggest installing react-router-hash-link which supports it.
The import can look like
import { HashLink as Link } from 'react-router-hash-link';
And you can have the same component attributes
<li className="nav-item">
<Link className="nav-link active" aria-current="page" to="/#productos">
Productos
</Link>
</li>
You need to enclose your <NavLink/>, <Link/>, <Routes/> and <Route/> components with a Router like <BrowserRouter/>.
So in App.js or another top level component, you need to do it like this:
const App = (props) => {
<BrowserRouter>
<Your sidebar component including NavLink elements/>
<Your app module including Route elements/>
</BrowserRouter>
}
I'm making a react project, where I need to put a navbar, I installed bootstrap with
npm install bootstrap
and I imported it in my index.js like this :
import 'bootstrap/dist/css/bootstrap.css';
I put the bootstrap's documentation navbar exemple in my component, but the button doesn't show the links.
Here's my code :
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<a class="navbar-brand" href="#">Hidden brand</a>
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
I tried to add :
import 'bootstrap/dist/js/bootstrap.js';
in my index.js file but, it didn't work
Do you have any idea, to solve this ?
First install bootstrap npm install bootstrap and than install jquery and popper.js to, npm install jquery popper.js.
Add all to index.js
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.bundle.min';
import $ from 'jquery';
import Popper from 'popper.js';
Updated, now the problem i'm seeing the navbar with format problems, shouldn't it be taking the CSS properties from bootstrap?
I Commited the project to make it easier: https://github.com/damianaguilarcogan/pfi_front_v4
Image: https://ibb.co/fdFTb4C
i just created a project but the navbar it's not loading, for sure it's a silly mistake but i'm not beeing able to find it.
I created the project, imported bootstrap in the index.html trought css links.
I created the folder components and inside ir navbar.js.
Afterwards I selected a common template from bootstrap navbar and Finally y called it from my app.js.
I don't get to see whats wrong, it should have already worked with this steps.
app.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import navbar from './components/navbar';
function App() {
return (
<div className="App">
<navbar />
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
navbar.js
import React from 'react';
class navbar extends React.Component {
render() {
return (
<nav className="navbar navbar-toggleable-md navbar-light bg-faded">
<button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<a className="navbar-brand" href="#">Navbar</a>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Link</a>
</li>
<li className="nav-item">
<a className="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form className="form-inline my-2 my-lg-0">
<input className="form-control mr-sm-2" type="text" placeholder="Search"></input>
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
)
}
}
export default navbar;
[1]: https://i.stack.imgur.com/zk8Lf.png
React expects components to be named with an uppercase. Try renaming the navbar to Navbar.
class Navbar extends React.Component
<Navbar/>
Changing your navbar to Navbar works. In addition there is a bootstrap wrapper for React that can be useful if you are just starting now. https://react-bootstrap.github.io/
Sorry if the title is kinda confusing.
I'm making an index page with a side and top navbar with the menu using react router, I have successfully loaded the content up but I want it to load in the main body, the middle of the page.
I've tried separating method like how I used JSP before but its not very effective so to speak
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Login from './Login';
import * as serviceWorker from './serviceWorker';
import Dropdown from 'react-bootstrap/Dropdown';
import { Link } from 'react-router-dom';
import { BrowserRouter, Route } from "react-router-dom";
import './index.css';
import AssetView from './AssetView';
import DepartmentView from './DepartmentView';
import LocationView from './LocationView';
import EmployeeView from './EmployeeView';
import AddDepartment from './AddDepartment';
import AddLocation from './AddLocation';
const routing = (
<BrowserRouter>
<div>
<nav class="navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Company name</a>
<ul class="navbar-nav px-3">
<li class="nav-item dropdown">
<Dropdown>
<Dropdown.Toggle variant="success" id="dropdown-basic">
Account
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">Informations</Dropdown.Item>
<Dropdown.Item href="#/action-2">Settings</Dropdown.Item>
<Dropdown.Item href="#/action-3">Signout</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</li>
</ul>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-md-2 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-Link active" href="#">
<span data-feather="home"></span>
Dashboard <span className="sr-only">(current)</span>
</a>
</li>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Assets Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<li className="nav-item">
<Link to="/AssetView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Assets
</a>
</Link>
</li>
</ul>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Company Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<ul className="nav flex-column mb-2">
<li className="nav-item">
<Link to="/DepartmentView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Departments
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/AddDepartment">
<a class="nav-link" href="#">
<span data-feather="file"></span>
Add New Department
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/LocationView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Locations
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/AddLocation">
<a class="nav-link" href="#">
<span data-feather="file"></span>
Add New Location
</a>
</Link>
</li>
</ul>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Employees Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<ul className="nav flex-column mb-2">
<li className="nav-item">
<Link to="/EmployeeView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Employees
</a>
</Link>
</li>
</ul>
</div>
</nav>
</div>
</div>
<Route exact path="/AssetView" component={AssetView} />
<Route path="/DepartmentView" component={DepartmentView} />
<Route path="/LocationView" component={LocationView} />
<Route path="/EmployeeView" component={EmployeeView} />
<Route path="/AddDepartment" component={AddDepartment} />
<Route path="/AddLocation" component={AddLocation} />
</div>
</BrowserRouter>
)
ReactDOM.render(routing, document.getElementById('root'));
serviceWorker.unregister();
This is how the page looks right now, I really wish to make the content of the page appear in the middle
https://photos.app.goo.gl/BSuZuvGhiQQetmit5
You mean, you have to create a dashboard-like app
Where top and sidebar is static and on click of the sidebar the middle part change
Then you can check this repo
https://github.com/designrevision/shards-dashboard-react
Here is the master page https://github.com/DesignRevision/shards-dashboard-react/blob/master/src/layouts/Default.js
import React from "react";
import PropTypes from "prop-types";
import { Container, Row, Col } from "shards-react";
import MainNavbar from "../components/layout/MainNavbar/MainNavbar";
import MainSidebar from "../components/layout/MainSidebar/MainSidebar";
import MainFooter from "../components/layout/MainFooter";
const DefaultLayout = ({ children, noNavbar, noFooter }) => (
<Container fluid>
<Row>
<MainSidebar />
<Col
className="main-content p-0"
lg={{ size: 10, offset: 2 }}
md={{ size: 9, offset: 3 }}
sm="12"
tag="main"
>
{!noNavbar && <MainNavbar />}
{children}
{!noFooter && <MainFooter />}
</Col>
</Row>
</Container>
);
DefaultLayout.propTypes = {
/**
* Whether to display the navbar, or not.
*/
noNavbar: PropTypes.bool,
/**
* Whether to display the footer, or not.
*/
noFooter: PropTypes.bool
};
DefaultLayout.defaultProps = {
noNavbar: false,
noFooter: false
};
export default DefaultLayout;
The correct answer has been replied by #ravibagul91, thank you.
I removed the class="sidebar-sticky" of the sidebar and add the .sidebar{position:absolute;top:0;bottom:0} in my index.css and it works just fine as the image I just uploaded
https://photos.app.goo.gl/m744U7R4mACAUXq37
Thank you #piyush-dhamecha for the recommendation, it is somewhat similar to what I need, but truth be told, it's still kinda advance for me, but I appreciate it and will keep it for future reference.
I am new to react. Im trying to make a navigation in my header.
i use bulma css :
"bulma": "^0.7.4",
And i import bulma file like this:
import 'bulma/css/bulma.css';
It is working for the most of the css but not with burger menu, the menu
is not collapsed when i click on the burger button
Here is my header code:
import React,{Component} from 'react'
import {NavLink} from 'react-router-dom';
import style from './style.css';
class Header extends Component{
render(){
return (
<React.Fragment>
<nav className="navbar is-fixed-top" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<a href="/" className="navbar-item" to="/">
<img src="" alt="" width={112} height={28}/>
</a>
<a role="button" className="navbar-burger burger " aria-label="menu" aria-expanded="false" data-target="mainNavbar">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="mainNavbar" className="navbar-menu">
<div className="navbar-start">
<NavLink exact activeClassName={style.active} to="/" className="navbar-item">
Home
</NavLink>
<NavLink activeClassName={style.active} to="/films" className="navbar-item">
Films
</NavLink>
</div>
</div>
</nav>
</React.Fragment>
);
}
}
export default Header;
The
data-target
attribute on the burger button is not triggering the menu.
Pleas let me know what im doing wrong.
Thanks
Bulma doesn't have a built in toggle event you have to create it manually by adding "is-active" class in "navbar-burger" and "navbar-menu" here I have achieved that with React Hooks
const [isActive, setisActive] = React.useState(false);
<nav className="navbar" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<a
onClick={() => {
setisActive(!isActive);
}}
role="button"
className={`navbar-burger burger ${isActive ? "is-active" : ""}`}
aria-label="menu"
aria-expanded="false"
data-target="navbarBasicExample"
>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div
id="navbarBasicExample"
className={`navbar-menu ${isActive ? "is-active" : ""}`}
>
<div className="navbar-start">
<a className="navbar-item">Home</a>
<a className="navbar-item">Documentation</a>
</div>
</div>
</nav>
As stated in another answer, additional js is required to make Bulma's burger menu work. Below is how I did it. The key in toggleBurgerMenu. Also, toggleBurgerMenu needs to be specified in the onClick event of each navbar-item so that the burger menu closes after a menu item is clicked.
import React from 'react';
import { Link } from 'react-router-dom';
const Navigation = (props) => {
function toggleBurgerMenu() {
document.querySelector('.navbar-menu').classList.toggle('is-active');
}
return (
<nav className="navbar" role="navigation" aria-label="main navigation">
<div className="navbar-brand">
<Link to="/" className="navbar-item">React App</Link>
<a role="button" className="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasic"
onClick={toggleBurgerMenu}>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasic" className="navbar-menu">
<div className="navbar-start">
<Link to="/about" className="navbar-item" onClick={toggleBurgerMenu}>About</Link>
<Link to="/contact" className="navbar-item" onClick={toggleBurgerMenu}>Contact</Link>
<Link to="/notes" className="navbar-item" onClick={toggleBurgerMenu}>Notes</Link>
</div>
</div>
</nav>
);
}
export default Navigation;
When you click on a burger, 'navbar-menu' should be 'navbar-menu is-active'.
In the opposite case, you should also remove 'is-active'.