1

So I am trying to render 4 new different pages which use these icons as links: enter image description here

// My navbar component:
import React, {Component} from 'react';
import {MenuItems} from './MenuItems'
import './Navbar.css' 
import {Link} from 'react-router-dom';

class Navbar extends Component{

    render(){
        return(
            <nav className="NavbarItems">        
                <ul className="nav-menu">
                {MenuItems.map((item, index) => {
                        return (
                            <Link to={item.url}>
                            <li key={index}>
                                <a className={item.cName} href={item.url}>
                                <img  width = "70" height = "70" alt ="element imgages" src={item.image}></img>
                                </a>
                            </li>
                            </Link>
                        )
                    })}
                   
                    
                </ul>
            </nav>


        )
    }
}
export default Navbar

In my App.jsx, I used the react-router-dom to route and link these:

import React, { Component } from 'react' ;
import Navbar from "./components/Navbar/Navbar";
import './App.css'
import { BrowserRouter as Router, Switch, Route} from "react-router-dom";

//navbar components
import Water from "./components/Water";
import Fire from "./components/Fire";
import Earth from "./components/Earth";
import Air from "./components/Air";


class App extends Component{
    render(){
        return(
            <Router>
                <div className="App">
                    <Navbar />
                </div>
                <Switch>               
                    <Route exact path="/water" component={Water}/>
                    <Route exact path="/earth" component={Earth}/>
                    <Route exact path="/fire" component={Fire}/>
                    <Route exact path="/air" component={Air}/>
                    </Switch>
                </Router>
        ) 
     }
}


export default App;

However, when I click on the images all that appears is this: enter image description here

import React, { Component } from 'react' ;



class Air extends Component{
    render(){
        return(
            <div>
                air
            </div>
        ) 
     }
}

export default Air

I am expecting the new rendered page to just show the text air, but it renders that div along with the main page HTML/CSS. Also I would like to add that I have also edited HTML directly inside my index.html.

<div id="root"></div>
    <div class="parallax-ATLA"></div>
    <div class="iframe-container">
      <iframe width=50% height=80% src="https://www.youtube-nocookie.com/embed/d1EnW4kn1kg" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
    <div class="parallax-LOK"></div>
    <div class="iframe-container">
      <iframe width=50% height=80% src="https://www.youtube-nocookie.com/embed/NBNpGCIavwA" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
  </body>
1
  • "I am expecting the new rendered page to just show the text air" this is what I see in your screenshot for your react code. What is "main page HTML/CSS"? Is that all the other markup outside the react app in the body? Commented Sep 24, 2020 at 23:46

2 Answers 2

1

You really shouldn't need to add HTML directly to the main index.html. Everything you do in React is rendered inside the root div. But since you have added HTML, everything React renders will now be on top of that added HTML. Whatever code you added directly can surely be placed into a custom component.

Sign up to request clarification or add additional context in comments.

2 Comments

So I should rewite my code such that the HTML is moved into a custom component. Sorry, I am new to this and someone in a React tutorial did this so I assumed it was fine . Thanks for letting me know.
Yes I would place that html in its own component. Then you can place that in any page you like.
1

Looks like you want the Air component to be the ONLY content of the page after you click the air icon. Then your NavBar should be inside the switch as part of a separate route.

I would do something like this:

import React, { Component } from 'react' ;
import Navbar from "./components/Navbar/Navbar";
import './App.css'
import { BrowserRouter as Router, Switch, Route} from "react-router-dom";

//navbar components
import Water from "./components/Water";
import Fire from "./components/Fire";
import Earth from "./components/Earth";
import Air from "./components/Air";

// Home page
const Home = () => (
  <div className="App">
    <Navbar />
  </div>
);


class App extends Component{
    render(){
        return(
            <Router>
                <Switch>               
                    <Route exact path="/" component={Home}/>
                    <Route exact path="/water" component={Water}/>
                    <Route exact path="/earth" component={Earth}/>
                    <Route exact path="/fire" component={Fire}/>
                    <Route exact path="/air" component={Air}/>
                </Switch>
            </Router>
        ) 
     }
}


export default App;

1 Comment

Hey, so I followed what you told me to do and as someone mentioned out in another answer, its also because I directly edited HTML code instead of rendering it through a component. But thank you for your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.