On the PostAd.js page, I am looking to remove the location bar, search bar, and sell button - only keeping the

I am looking to customize the PostAd.js page by displaying only the logo and hiding the location bar, search bar, and sell button. I have included my code below for reference. As a newcomer learning React.js, I am striving to showcase just the logo on the PostAd.js page.

Refer to the image below:

Header.js

    import React, { Component } from 'react';
    import { Link } from "react-router-dom";
    
    function Logo() {
            return (
                <a className="navbar-brand logo" href="#">
                    <img src={require("../ui/logo.png")} />
                </a>
            );
    }
    
    function SearchAndLocation(props) {
            return (
                <div className="collapse navbar-collapse" id="navbarSupportedContent">
    
                <ul className="navbar-nav flex-2 pr-3">
                    <li className="input-group input-group-lg location mr-4 flex-1">
                        <div className="input-group-btn">
                            <button className="fas fa-search search-ico"></button>
                        </div>
                        <input type="text" className="form-control" placeholder="Pakistan" />
                        <div className="input-group-btn">
                            <button className="fas fa-chevron-down ico"></button>
                        </div>
                    </li>
    
                    <li className="input-group input-group-lg search flex-2">
                        <input type="text" className="form-control" placeholder="Find Mobile, Car ,laptop" />
                        <div className="input-group-btn">
                            <button className="fas fa-search ico"></button>
                        </div>
                    </li>
                </ul>
                <form className="form-inline my-2 my-lg-0">
                    <h6 className="mr-sm-2 login" >Login</h6>
                    <button className="my-2 my-sm-0 fas fa-plus sell">&nbsp;<Link to={"/postad"}>SELL</Link></button>
                </form>
            </div>
            );
    }
    
    
    
    function Header() {
            return (
                <nav className="navbar navbar-expand-lg navbar-light bg-light">
                    <Logo />
                    <SearchAndLocation />
                </nav>
            );
    }
    
    export {
        Header,
        Logo,
        SearchAndLocation
    };

PostAd.js

     import React, { Component } from 'react';
        import { Header } from "../components/Header";
        
        function PostAd() {
            return (
                <Header></Header>
            );
        }
        
        export default PostAd;

router.js

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


class AppRouter extends Component {
  render() {
    return ( 
      <div>
        <Router>
          <Route exact path='/postad' component={PostAd} />
        </Router>

      </div>
    );
  }
}

export default AppRouter;

Answer №1

To implement conditional rendering, you can create a variable and set it to either false or true based on your requirement. This would allow you to hide or display a component using the logical AND operator.

function Header() {
        const displaySearchBar = false
        return (
            <nav className="navbar navbar-expand-lg navbar-light bg-light">
                <Logo />
                {displaySearchBar &&
                    <SearchAndLocation />
                }
            </nav>
        );
}

Answer №2

Building with React is like playing with Lego blocks - you can easily compose components and remove them from the screen as needed.

Here's a task for you:

Delete the SearchAndLocation component from the Header component.

Follow these steps:

function Header() {
        return (
            <nav className="navbar navbar-expand-lg navbar-light bg-light">
                <Logo />
                <SearchAndLocation />     <--- delete this line
            </nav>
        );
}

If you only want to hide it in PostAd, here are 2 solutions:

A. Create a new Header specifically for use in PostAd.

B. Modify the Header to check the current route and hide the "SearchAndLocation" component if it's "/postad".

Let's stick with solution A for simplicity. Here's what you need to do:

  1. Create a new header component, perhaps named NewHeader, with the same content as Header but without "SearchAndLocation".
  2. Export the NewHeader and use it in the PostAd component.
  3. You're all set!

For solution B, follow these steps:

  1. In the Header component, utilize the useLocation hook provided by 'react-router-dom' (assuming you're using it).
  2. Retrieve the current path using useLocation.
  3. Add an if-else rendering logic: if path === /postad, don't render the "SearchAndLocation" component; otherwise, render it.
  4. All done!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After pressing the submit button once, I want to console.log the new value and remove the previously mapped items. However, this functionality is not working as expected

Just starting out with React and encountering a couple of issues: After clicking the submit button, I want to console log the input and display the mapped data. However, currently, I have to click the button twice to see both outputs. I am looking to cle ...

Show the current time using Moment.js

I am currently working on developing a clock component that displays the current time in real-time. Issue: The initial time is correctly displayed when the page loads (HH:mm A), but the clock does not update dynamically. clock.component.ts : import { ...

Use CSS to style text when it falls on a "/" within a table

I currently have a table with a row that displays like this: word hi Someword/AnotherWord/LastWord My goal is to use CSS to break the text at the "/" symbol like this: hi<br>Someword<br>/AnotherWord<br>/LastWord However, the curr ...

Utilizing Javascript and CSS for horizontal alignment from left to right

I have a JavaScript function that generates a list of store locations and creates a DIV beneath the map. Currently, the items are displayed top to bottom on the page. However, I would like to change the layout so that the items are shown as 3 in a row fro ...

Whenever a socket event occurs, the state of Nextjs is automatically reset

I attempted to implement chat functionality into my Nextjs app. Despite following the documentation, the chat log gets reset every time a socket event occurs. Below is the code snippet: import { useState, useEffect } from 'react' import io from & ...

Is there a way to delay the closing of the browser until the server call is finished?

I am attempting to invoke a service that updates data upon browser close or tab close. The issue I am facing is that the service call uses $http and is asynchronous, so I believe that the browser unload event is cancelling the call and closing the browser. ...

The TypeScript declarations for the scss module are malfunctioning

Just recently, I set up a React project using rollup. Below is the configuration file for my rollup setup: rollup.config.js import serve from "rollup-plugin-serve"; import livereload from "rollup-plugin-livereload"; import babel from &q ...

The width and height properties in the element's style are not functioning as expected

let divElement = document.createElement("div"); divElement.style.width = 400; divElement.style.height = 400; divElement.style.backgroundColor = "red"; // num : 1 divElement.innerText = "Hello World "; // num : 2 document.body.append(divElement); // Af ...

Display targeted information upon clicking a designated division using JavaScript or jQuery

I'm working on a FAQ page and I want to display a specific answer when a particular question is clicked. If you'd like to see it in action, here's a fiddle: http://jsfiddle.net/hdr6shy9/ Below is the code I'm using: HTML: <div cl ...

Utilizing Vue-i18n for language translations directly within a JavaScript file, rather than using

Is there a way to utilize the .js file for translations instead of the .json file? I attempted changing: const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i) to const locales = require.context('./loca ...

Show information upon opening

I am currently working on a project that involves 4 different divs, and I need the content of each div to be displayed based on dropdown selection. I found a code snippet that was close to what I needed, but after trying to adjust it, I haven't been a ...

Add a touch of shadow to a stationary top banner as you scroll through the page

Is there a way to gradually fade in a shadow while scrolling, so that the shadow only reaches an opacity of 0.5 after scrolling a certain number of pixels? I've come across solutions to add a shadow or class to the top banner based on content position ...

Upgrading from Material UI Version 0.20 to v4.3.0: The Ultimate Transformation

I have been working on an application that was built using an older version of material UI (V0.20). Now, I am considering upgrading to the latest version of material UI and adding new modules to the application. However, I am concerned about the potentia ...

Modifying Element Values with JavaScript in Selenium using C#

As a newcomer to automation, I am facing a challenge with automating a web page that has a text field. I initially attempted using driver.FindElement(By.XPath("Xpath of elemnt").SendKeys("Value"); but unfortunately, this method did not work. I then resor ...

Looking for a comprehensive calculation that takes into account various input values?

I need to display the List Price at the bottom of the form so users are aware of the cost to list their item. Within a php file, I have defined price brackets. For example, if the listing price is £150.00, it falls between £100 and £199.99 and thus nee ...

How to Retrieve the Url of a Clicked Element using CSS Property

Is it possible to retrieve the URL when a link is clicked using CSS alone, without relying on JavaScript or jQuery? ...

CSS - Organization of Rows and Columns

I am in the process of designing a website that will feature news content, and I would like to give it a layout similar to a reddit news box. The layout would resemble the following structure. I am using angular material and CSS exclusively, without any fr ...

my divs are not being affected by the max-width property and are retaining their

I've been learning CSS and HTML through an interesting tutorial on Udacity. Here's the code I've been working on: .image{ max-width: 50%; } .app{ display: flex; } .description{ color: red; max-width: 705px; } <h1 class="title" ...

Can HTML and CSS be used to create button text that spans two lines with different fonts?

When working with HTML attribute values, I am facing an issue where I can't get button text to display in two lines with different font sizes. I have attempted using whitespace in CSS for word wrap, but this solution does not solve my problem. I also ...

Remain concealed once the lights dim

Looking for a way to fade out a logo in a preloader div after a call, but having issues with it becoming visible again once fully faded out. Ideally, I would like to keep it faded out or set it to display none using only CSS, although I can use jQuery if n ...