0

I have started doing a project in react js and i have been trying to wrap my head around routing and other concepts, i have had a problem with the routing and the problem is, when i run the project, there is a button in home page and then i click on that button, it should go to other pages, in my case, when the button is clicked, the content appears right on the bottom the home page.

App.js

import * as React from 'react';
import {BrowserRouter as Route, Routes} from "react-router-dom";
import { LandingPage, NavBar, Footer } from './Containers';
import './App.css';
import Scroll from './scroll';

function App() {
  return (
    <div className="App">
        <div>
            <NavBar />
            <LandingPage />
             <Routes>
               <Route path="/scroll" element={<Scroll />} />
             </Routes>
            <Footer />
        </div>
    </div>
  );
}
export default App;

landingPage.js

import React from "react";
import { Route, Routes, useNavigate, Link } from "react-router-dom";
import Scroll from "../../scroll";
import "./landingPage.css";

const LandingPage = () => {
  const Navigate = useNavigate();
  
  const handleClick = () => {
    Navigate('/scroll');
  }
  return (<>
      <div >
        <div >
          <div >
              <h1 >Amet minim mollit non deserunt ullamco est sit aliqua dolor doamet sint. Velit officia</h1>
              <p >Amet minim mollit non deserunt ullamco est sit aliqua dolor doamet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.</p>
            <h3>
              <div >
              <Link to={"./scroll"}>
                <button onClick={()=>handleClick()}>
                  <bold>Get Started</bold>
                </button>
              </Link>
              </div>
            </h3>
          </div>
         </div >
        </div>
 </>
  );
};

export default LandingPage;

scroll.js

import React from 'react'
import HorizontalScroll from 'react-scroll-horizontal';

import './scroll.css';

import { Execution1, Execution2, Execution3, Execution4, Execution5, Execution6, Onboarding, PDP } from './Containers';

function scroll() {
    const Object = {height: `77%`, width: `100vw`}
  return (
    <>
      
      <HorizontalScroll reverseScroll={true} style={Object} >
        <PDP className="main"/>
        <Onboarding className="main" />
        <Execution1 className="main" />
        <Execution2 className="main" />
        <Execution3 className="main" /> 
        <Execution4 className="main" />
        <Execution5 className="main" />
        <Execution6 className="main" />
      </HorizontalScroll>
    </>
  )
}

export default scroll

The ones in ./Containers are components like landing page. What i wanted to do was, when we click in the button of LandingPage, the whole component from scroll.js schould appear and they should be rendered horizontally.

but, as of now. i can only see the urls changing but not the contents.

My folder structure is like this. enter image description here

  • The code you've linked to doesn't render anything but a single "page" with everything on it. If you want to simulate a multi-page app then you'll need to actually implement client-side routing & navigation. Please try to [edit] the post to include a [mcve]. – Drew Reese Jan 23 '23 at 22:51
  • I have edited the question and provided the essential ones to see the problem. – joseph thomas Jan 24 '23 at 09:35

2 Answers2

1

create a route for PDP. In your App.js, add this code.

import React from "react"
import NavBar from "./NavBar"
import PDPfrom "./PDP"
import {BrowserRouter as Router, Switch, Route} from "react-router-dom";

function App() {
    return (
        <Router>
        <div>
            <NavBar />
            <Route path="/PDP" component={PDP} />
        </div>
        </Router>
        )
}

export default App

Now in your landing Page where you're using Button, Add this

import { Link } from 'react-router-dom'

    <Link to={"./PDP"}>
        Get Started  
    </Link>
Nimra Haider
  • 119
  • 3
  • 11
  • Does this means that when ever the page loads the page will automatically be in home page and it will go to PDP page when only i click the Get Started button? – joseph thomas Jan 23 '23 at 09:36
  • I tried the above solution, but the only thing that changed was the url, the page content does not render and it throws another error:- Matched leaf route at location "/PDP" does not have an element. This means it will render an with a null value by default resulting in an "empty" page. – joseph thomas Jan 23 '23 at 10:02
1

You've mixed up the component imports, specifically you've imported BrowserRouter as Route and there's no route component.

Fix the BrowserRouter component import and then also import the Route component to render the Scroll component on. Ensure the router wraps all the components that need to access the routing context (i.e. Link, and Route, etc). You might also want to render LandingPage on the home path "/" so it's not also rendered when navigating to the "/scroll" page.

import * as React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { LandingPage, NavBar, Footer } from './Containers';
import './App.css';
import Scroll from './scroll';

function App() {
  return (
    <div className="App">
      <Router>
        <NavBar />
        <Routes>
          <Route path="/" element={<LandingPage />} />
          <Route path="/scroll" element={<Scroll />} />
        </Routes>
        <Footer />
      </Router>
    </div>
  );
}

export default App;

In the LandingPage don't attach an onClick handler to the button element nested in the Link component, just let the Link handle issuing the navigation action to "/scroll".

import React from "react";
import { Link } from "react-router-dom";

const LandingPage = () => {
  return (
    <div>
      <div>
        <div>
          <h1>Amet minim mollit .... officia</h1>
          <p>Amet minim mollit .... sunt nostrud amet.</p>
          <h3>
            <div>
              <Link to="/scroll">
                <bold>Get Started</bold>
              </Link>
            </div>
          </h3>
        </div>
      </div >
    </div>
  );
};

export default LandingPage;
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • What if i need to change something in navbar, when i click on the and i also want to render the page at the same time? – joseph thomas Jan 30 '23 at 04:02
  • This works, thank you. But the problem is i can't see any component in the screen, The footer component is just below the navbar, when inspecting it with react developer tools, its all there. Is this because of the router? or is it because of the css?? – joseph thomas Jan 30 '23 at 04:20
  • 1
    @josephthomas The routes are rendered between the navbar and footer. Are you saying the routed content is rendering somewhere else? It *could* be an issue caused by CSS if the CSS is messing with or changing display rules. If you want, see if you can create a *running* [codesandbox](https://codesandbox.io/) demo that reproduces the rendering issue that we could inspect live. – Drew Reese Jan 30 '23 at 04:27
  • 1
    @josephthomas As for the first comment, you only need to render `Link` components to paths you are rendering routes for, `react-router` will handle the route matching. – Drew Reese Jan 30 '23 at 04:30
  • https://codesandbox.io/s/relaxed-ganguly-wy1yfj?file=/src/Containers/NavBar.jsx&resolutionWidth=1400&resolutionHeight=1248 This is the link to the codesandBox and the thing is i only want the button on the navbar to appear after i click on the Get Started button. – joseph thomas Jan 30 '23 at 05:48