Home > Software design >  code from guided project not rendering on my project
code from guided project not rendering on my project

Time:11-15

(first ever question asked please go easy on me)

HI I've copy and pasted a component from a guided project to see that nothing has rendered.. thing's I've done:

  1. imported same modules
  2. replaced images
  3. changed app.js to only show this component
  4. replaced styling with styling from guided project (guided project works fine in other tab)

what checks do you do after nothing has rendered?

NavBar2.js

import { useState, useEffect } from "react";
import { Navbar, Nav, Container } from "react-bootstrap";
import { HashLink } from 'react-router-hash-link';
import {
  BrowserRouter as Router
} from "react-router-dom";

const NavBar2 = () => {

  const [activeLink, setActiveLink] = useState('home');
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => {
      if (window.scrollY > 50) {
        setScrolled(true);
      } else {
        setScrolled(false);
      }
    }

    window.addEventListener("scroll", onScroll);

    return () => window.removeEventListener("scroll", onScroll);
  }, [])

  const onUpdateActiveLink = (value) => {
    setActiveLink(value);
  }

  return (
    <Router>
      <Navbar expand="sm" className={scrolled ? "scrolled" : ""}>
        <Container>
          <Navbar.Brand href="/">
            <img src= "src/assets/Adobe_Photoshop_CC_icon.png" alt="Logo" />
          </Navbar.Brand>
          <Navbar.Toggle aria-controls="basic-navbar-nav">
            <span className="navbar-toggler-icon"></span>
          </Navbar.Toggle>
          <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="ms-auto">
              <Nav.Link href="#home" className={activeLink === 'home' ? 'active navbar-link' : 'navbar-link'} onClick={() => onUpdateActiveLink('home')}>Home</Nav.Link>
              <Nav.Link href="#skills" className={activeLink === 'skills' ? 'active navbar-link' : 'navbar-link'} onClick={() => onUpdateActiveLink('skills')}>Skills</Nav.Link>
              <Nav.Link href="#projects" className={activeLink === 'projects' ? 'active navbar-link' : 'navbar-link'} onClick={() => onUpdateActiveLink('projects')}>Projects</Nav.Link>
            </Nav>
            <span className="navbar-text">
              <div className="social-icon">
                <a href="#"><img src="src\assets\Adobe_Photoshop_CC_icon.png" alt="" /></a>
                <a href="#"><img src='src\assets\Adobe_Photoshop_CC_icon.png' alt="" /></a>
                <a href="#"><img src='src\assets\Adobe_Photoshop_CC_icon.png' alt="" /></a>
              </div>
              <HashLink to='#connect'>
                <button className="vvd"><span>Let’s Connect</span></button>
              </HashLink>
            </span>
          </Navbar.Collapse>
        </Container>
      </Navbar>
    </Router>
  )
}

export default NavBar2

App.js

import logo from './logo.svg';
import './App.css';
import NavBar2 from "./components/NavBar2";

function App() {
  return (
    <div className="App">
      <NavBar2/> 
    </div>
  );
}

export default App;

ok here are errors i get but i dont see any reference to code i've written/copy.pasted

{
  "name": "portfolio-website",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.2.2",
    "react": "^18.2.0",
    "react-bootstrap": "^2.6.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.4.3",
    "react-router-hash-link": "^2.4.3",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

expected render of a navigation bar, received a blank screen

CodePudding user response:

I posted your code into a StackBlitz and changed

import { useState, useEffect } from "react";

to

import React, { useState, useEffect } from "react";

then ran an npm install to make sure I had the latest of all your packages. This is rendering for me. Double check all your imports and versions of packages.

  • Related