React won't render my components using router

Viewed 76

I am very new to react and trying to work on my first website. I have tried to seek my problem online, I've encountered similar questions to mine but could not figure out my exact problem.

My layout of components are in a scroll down style (portfolio), when I try route for example to my contact component it wont render unless I refresh my page. also instead of scrolling down to component it will pop up at the top .(Hope I am clear)

My App Function

import { useState } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import './App.css';
import Contact from './Components/Contact/Contact';
import Form from './Components/FormArea/Form/form';
import Introdoction from './Components/Introdoction/Introdoction';
import NavBar from './Components/NavBar/NavBar';
import NavigationBar from './Components/NavigationBarHeader/NavigationBar/NavigationBar';
import Portfolio from './Components/Portfolio/Portfolio';
import Routing from './Components/Routing/Routing';
import Skills from './Components/Skills/Skills';



function App() {
 
  return (
    <BrowserRouter>
    <Switch>
    <div className="App">
      
      <NavigationBar/>
     
      <section className="section">
      
        <Route path="/contact" component={Contact} exact/>
        <Introdoction/>
        <Portfolio/>
        <Skills/>
        {/* <Contact/> */}
        <Form/>
      </section>
    </div>
    </Switch>
  </BrowserRouter>
  );
  
}

export default App;

I am trying to configure "Contact" component for this example.

My Menu/Navbar Component

import { Component, MouseEventHandler } from "react";
import {MenuItems} from "../MenuItems/MenuItems";
import "./NavigationBar.css";
import icon from '../../../Assets/icon.png'
import  {Button}  from "../Button/Button";
import { NavLink, Redirect, Route, Switch, useHistory } from "react-router-dom";
import Contact from "../../Contact/Contact";


class NavigationBar extends Component {
    state = {clicked : false}

    handleClick = () => {
        this.setState ( { clicked: !this.state.clicked } );
      };

    public render(): JSX.Element {
        return (
          
            <nav className="NavbarItems ">
                <img className="navbar-logo"src={icon } />
                <div className="menu-icon" onClick={this.handleClick}>
                    <i className={this.state.clicked ? 'fas fa-times' : 'fas times'}></i>

                </div>

                <ul className={this.state.clicked ? 'nav-menu active' : 'nav-menu'}>

                    {MenuItems.map((item,index) => {
                        return (

                            <li key={index}>

                        <NavLink to={item.url} className={item.cName} >{item.title} </NavLink>
                        
                            </li>
                        )
                    })}

                    
                </ul>
                <Button>Sign up</Button>
               
            </nav>
        );
    }
}



export default NavigationBar;

ItemMenu Component

import { NavLink } from "react-router-dom";
import "./MenuItems.css";

export const MenuItems = [
    {
        title: 'Home',
        url: '/Home',
        cName: 'nav-links'
    },
    {
        title: 'Introduction',
        url: '/introduction',
        cName: 'nav-links'
    },
    {
        title: 'Skills',
        url: '/skills',
        cName: 'nav-links'
    },
    {
        title: 'Projects',
        url: '/projects',
        cName: 'nav-links'
    },
    {
        title: 'Contact',
        url: '/contact',
        cName: 'nav-links'
    },
];

I could add more information if needed. I hope my question is clear on what my problem is.

Thanks.

1 Answers

If I understood your question correctly, you want one single page, with everything below each other, and when you press a link, it scrolls you down to that place. In that case I would use:

<a href="#contact"></a>
<Contact id="contact" />

react-router creates new sub pages, which you don't want (if I understood you correctly)

Related