2

I tried nesting a <button> tag into a <Link> tag with the link routing to another page, but when I click the button, nothing happens. This is for a pricing card, and the code is in pricecard.js

App.js:

import logo from './logo.svg';
import './App.css';
import donenoti from './donenoti.js';
import errornoti from './errornoti.js';
import infonoti from './infonoti.js';
import pricingCard from './pricecard.js'
import personCard from './personcard.js'

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <p className="titleText">mushyUI</p>
        <img src={logo} className="App-logo" alt="logo" />
        <h1 className="header">
          Open-Source UI Elements for everyone!
        </h1>
        <p>Github -- <a href="https://github.com/MushyToast/ui">Github</a></p>
        {donenoti("title", "description")}
        {errornoti("title", "description")}
        {infonoti("title", "description")}
        {pricingCard("professional edition", "$19.99", "It is very much professional", "month", "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "https://www.youtube.com/watch?v=dQw4w9WgXcQ")}
        {personCard("Quandale Dingle", "I am a person", "https://mushytoast.tech/assets/imgs/goofycapybara.png")}
      </header>
    </div>
  );
}

export default App;

pricecard.js:

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

function pricingCard(title, price, description, timeunit, buylink, learnmorelink) {
  return (
    <div className="pricingcard">
      <h2 className="priceTitle">
        {title}
      </h2>
      <h1 className="price">
        {price}
        <p className="priceTimeUnit">
          /{timeunit}
        </p>
      </h1>
      <p className="priceDescription">
        {description}
      </p>
            
      <span className="priceCardButtons">
        <a href={buylink}> {/*change to your own link */}
          <button className="priceButton">
            Buy Now
          </button>
        </a>
        <Link to='./otherpage'>
          <button className="learnMoreButton">
            Learn More
          </button>
        </Link>
      </span>
    </div>
  )
}

export default pricingCard;

otherpage.js:

import React from 'react';
import { BrowserRouter } from 'react-router-dom';

export default function OtherPage() {
  return (
    <div>
      <h1>Other Page</h1>
    </div>
  );
}

root = document.getElementById('root');
ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  root
);

index.js:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { BrowserRouter } from 'react-router-dom'
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

reportWebVitals();

I tried adding a rendering tag in the otherpage.js, but that made no difference, and I tried adding a .js to the otherPage in the Link to=''

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
mushytoast
  • 38
  • 5
  • What exactly isn't working as expected? What does "it's not working" mean, specifically? What are you expecting to happen when the button/link is clicked? Please [edit] to clarify what the specific issue is. – Drew Reese Mar 29 '23 at 18:17
  • Sorry. Nothing happens when I click the button – mushytoast Mar 29 '23 at 19:34
  • What are you expecting to happen when the link is clicked? Does the URL in the address bar at least update to `"/otherpage"`? Are you rendering a `Route` on path `"/otherpage"` that you are targeting with the link? – Drew Reese Mar 29 '23 at 19:44
  • I am quite new to react, so sorry for this. I am expecting to go to a whole other page, not just render a component. Sorry for asking, but how do I render a Route? – mushytoast Mar 29 '23 at 19:59
  • Oh, and before I changed to correspond with the answer given, it did change the url bar. – mushytoast Mar 29 '23 at 20:03

1 Answers1

0

The code isn't rendering a Route to link or navigate to. The code is also incorrectly using regular Javascript functions to render content instead of React functions so they are incorporated as part of the normal React component lifecycle.

Render routes to render specific content matched by the URL

The OtherPage component should be rendered on a Route in the App component instead of directly trying to inject itself into the DOM. Move the header content also into its own route on "/".

import { Routes, Route } from 'react-router-do';
import OtherPage from '../path/to/OtherPage';

...

function App() {
  return (
    <div className="App">
      <Routes>
        <Route
          path="/"
          element={(
            <header className="App-header">
              ...
            </header>
          )}
        />
        <Route path="/otherpage" element={<OtherPage />} />
        { /*... other routes ... */ }
      </Routes>
    </div>
  );
}

Convert donenoti, errornoti, infonoti, etc to React components

Convert these Javascript functions to React function components. Keep in mind that React components are Capitalized.

Example:

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

function PricingCard({
  title,
  price,
  description,
  timeunit,
  buylink,
  learnmorelink
}) {
  return (
    <div className="pricingcard">
      <h2 className="priceTitle">{title}</h2>
      <h1 className="price">
        {price}
        <p className="priceTimeUnit">/{timeunit}</p>
      </h1>
      <p className="priceDescription">
        {description}
      </p>
            
      <span className="priceCardButtons">
        <a href={buylink}> {/*change to your own link */}
          <button type="button" className="priceButton">
            Buy Now
          </button>
        </a>
        <Link to="/otherpage">
          <button type="button" className="learnMoreButton">
            Learn More
          </button>
        </Link>
      </span>
    </div>
  )
}

export default PricingCard;
...
import Donenoti from './Donenoti';
import Errornoti from './Errornoti';
import Infonoti from './Infonoti';
import PricingCard from './Pricecard';
import PersonCard from './Personcard';

function App() {
  return (
    <div className="App">
      <Routes>
        <Route
          path="/"
          element={(
            <header className="App-header">
              <p className="titleText">mushyUI</p>
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="header">
                Open-Source UI Elements for everyone!
              </h1>
              <p>
                Github -- 
                <a href="https://github.com/MushyToast/ui">Github</a>
              </p>
              <Donenoti title="title" description="description" />
              <Errornoti title="title" description="description" />
              <Infonoti title="title" description="description" />
              <PricingCard
                title="professional edition"
                price="$19.99"
                description="It is very much professional"
                timeunit="month"
                buylink="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
                learnmorelink="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
              />
              <PersonCard
                title="Quandale Dingle"
                description="I am a person"
          link="https://mushytoast.tech/assets/imgs/goofycapybara.png"
              />
            </header>
          )}
        />
        ...
      </Routes>
    </div>
  );
}
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • I converted the `PricingCard` to a react component, and added the routes in the App.js file after the React component definition: ```js } /> ``` When I click the button, still nothing happens. However, it does change the URL – mushytoast Mar 30 '23 at 15:07
  • @mushytoast Did that get your issue resolved and the app working as you expect? – Drew Reese Mar 30 '23 at 15:11
  • No, when I click the button, still nothing happens. The URL bar does change. Also, I had to remove the render part of `otherPage.js` because it threw an error during compiling. – mushytoast Mar 30 '23 at 15:16
  • Wait wait, something super odd just happened. The otherPage part seems to render, even after I took the render tag away, probably because of the routes, but, it renders as part of the page, not a separate page how I want it. – mushytoast Mar 30 '23 at 15:17
  • @mushytoast Ah, sorry, I should have explicitly mentioned you needed to remove the other `ReactDOM.render` you had in the `OtherPage` file. Unless you are referring to the header content I see only the one page. Are you wanting the header content and the `OtherPage` content to render on two discrete "pages"? – Drew Reese Mar 30 '23 at 15:20
  • Yeah, that's what I've been wanting this whole time. Sorry if I didn't specify that specifically in the initial answer, but I've been wanting "otherPage.js" to render on a whole seperate page. – mushytoast Mar 30 '23 at 15:22
  • @mushytoast No worries, it's an easy change. I've updated my answer. – Drew Reese Mar 30 '23 at 15:23
  • I updated my code, but now nothing renders at all. – mushytoast Mar 30 '23 at 16:29
  • If you want to see the repository code, here it is: https://github.com/MushyToast/ui – mushytoast Mar 30 '23 at 16:30
  • @mushytoast I can take a look at your repo, but in the meantime, do you think you could create a ***running*** [codesandbox](https://codesandbox.io/) demo of your code that reproduces the issue we could inspect live? – Drew Reese Mar 30 '23 at 16:31
  • I know this sounds super dumb, but I'm on my school chromebook right now and they block the website. Probably because people make proxies on there. I can do it when I get home (which is in like 3 hours). I hope you don't mind waiting. – mushytoast Mar 30 '23 at 16:49
  • @mushytoast Not dumb at all. I have a work VPN that blocks CSB for some reason, so I have to hop off the VPN to do anything CSB related. I'm always around, so no rush. – Drew Reese Mar 30 '23 at 16:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252884/discussion-between-mushytoast-and-drew-reese). – mushytoast Mar 30 '23 at 23:02