Tips on combining react material ui Link and react router Link for seamless integration

My navigation bar has a routing issue that I'm trying to solve. Currently, the router only responds when the route is manually typed in the search bar. Clicking on the tabs does not redirect properly, except for the home button tab.

import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
Typography from '@material-ui/core/Typography';
InputBase from '@material-ui/core/InputBase';
SearchIcon from '@material-ui/icons/Search';
import  Link from '@material-ui/core/Link';
import logo from '../assets/mainLogo2.png';
Avatar from '@material-ui/core/Avatar';
CssBaseline from '@material-ui/core/CssBaseline';
import useStyles from './NavigatorStyles';
import { Link as Nav, Route, Switch, Redirect } from 'react-router-dom';
Home from '../home/Home';
import AnimeList from '../animeList/AnimeList'
import NotFound from '../404/NotFound';

function Navigator() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
        <CssBaseline />
            <AppBar position="static" color="primary" className={classes.appBar}>
            <Toolbar className={classes.toolbar}>
                <Avatar src={logo} />
                <Typography variant="h6" color="inherit" noWrap className={classes.toolbarTitle}>Anime Search</Typography>
                <nav>
                    <Link component={Nav} to="/" variant="button" color="inherit" className={classes.link}>
                        Home 
                    </Link>
                    <Link compoent={Nav} to="/animeList" variant="button" color="inherit" className={classes.link}>
                        Anime List
                    </Link>

                </nav>

                <div className={classes.search}>
                    <div className={classes.searchIcon}>
                        <SearchIcon />
                    </div>
                    <InputBase
                        placeholder="Search…"
                        classes={{
                            root: classes.inputRoot,
                            input: classes.inputInput,
                        }}
                        inputProps={{ 'aria-label': 'search' }}
                    />
                </div>
            </Toolbar>
        </AppBar>
                <Switch>
                    <Redirect exact from="/" to="/home" />
                    <Route component={Home} path="/home" />
                    <Route component={AnimeList} path="/animeList" />
                    <Route component={NotFound} path="*" />
                </Switch>
    </div>
  );
}

export default Navigator;

I had to change the Link for the react router to "Nav" because there was a naming conflict between both elements.

Answer №1

There's a small error in the component code here:

<Link compoent={Nav} to="/animeList" variant="button" color="inherit" className={classes.link}>

I highly recommend checking out the documentation on composition link

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

Begin with a blank canvas and wait for inspiration to shape your journey

Is there a similar feature in REACT to what Angular offers with nullable types? In Angular, you can use something like crawlList?.jobsList {crawlList.jobsList.map(crawl => ( <div>test</div> ))} The issue I'm facing is that I set jo ...

implementing smooth scrolling with a specified duration

I decided to use react-router-hash-link over React-Scroll in order to link to another component and implement smooth scrolling. I've managed to adjust the scroll offset, but I'm stumped on how to add a duration to the scroll animation. Can anyone ...

Is there a way to interact with a Bootstrap 5 dropdown in React without triggering it to close upon clicking?

I'm currently working on creating a slightly complex navigation bar using Bootstrap 5 and ReactJS. The issue I'm encountering involves the dropdown menu within the nav bar. Whenever I click inside the dropdown, even if it's just non-link te ...

Tips for successfully sending an API request using tRPC and NextJS without encountering an error related to invalid hook calls

I am encountering an issue while attempting to send user input data to my tRPC API. Every time I try to send my query, I receive an error stating that React Hooks can only be used inside a function component. It seems that I cannot call tRPC's useQuer ...

Implement two useState hooks in React, where the value of one depends on the value of

Utilizing two useState hooks, one for object A to initialize object B, is my current approach. However, I am encountering an issue where my render function detects an empty B array, resulting in a brief white screen display. Removing the initialization on ...

The new mui v5 Dialog is having trouble accepting custom styled widths

I am facing an issue with my MUI v5 dialog where I cannot seem to set its width using the style() component. import { Dialog, DialogContent, DialogTitle, Paper, Typography, } from "@mui/material"; import { Close } from "@mui/icons- ...

Array Filtering with Redux

I have come across similar queries, but I am still unable to find a solution. While typing in the search box, the items on the screen get filtered accordingly. However, when I delete a character from the search box, it does not show the previous items. For ...

How to Eliminate Image Flickering While Loading in a React Component

Currently, I am developing a React component that takes an imageUrl and displays it on a canvas with a deliberate 2-second delay to mimic loading time for larger images. However, I have encountered a problem: when the imageUrl changes in the parent compone ...

When using NextJS, the dynamically generated HTML elements do not get affected by CSS repaint

Recently, I encountered an issue while working on a Next.js project involving the addition of an external script that uses vanilla JavaScript to dynamically create nodes. Despite importing global CSS at the top of my _app.js file, I noticed that the styles ...

Create a customized HTML popup featuring various packages

I am struggling to create an HTML popup within a React component due to some errors Here is the code snippet: <div> <button class="toggle">Button</button> <div id="link-box"> <ul> ...

Issue with React Routes only occurring in the production website

I'm encountering an issue on my personal website that only occurs in production, but not in my local environment. Here's the situation: I have set up the routes as follows const Routes = () => ( <Router> <Route exact path=&quo ...

Error encountered while running the command "npm start" for a

** Visual Studio 2022 Developer Command Prompt v17.0.0 ** Copyright (c) 2022 Microsoft Corporation C:\Users\johndoe\Desktop\project1\create-react-app\docusaurus\website>npm start @ start C:\Users\johndoe&bso ...

Tips for positioning an MUI element inside a container to extend to the edge of the browser window

Currently, I'm working on a project with a Material-UI (MUI) container that contains a Stack element. Within this Stack, there are two Box elements displayed in a row, each set to take up 50% of the width. In my design, I want the second box to break ...

Using React, a link to the same component is created, but a subcomponent is mistakenly using an outdated version of

Here, we have a SubComponent and a MainComponent created to showcase an image collection. The Subcomponent allows you to toggle between pictures in the collection using the onclick() event. The MainComponent also includes links to other collections, which ...

Why do I always find myself needing to verify variable values in react?

When working with React, I sometimes encounter the profile is null error upon page refresh. To address this issue, I have resorted to enclosing all the code inside if statements like so: useEffect(() => { getCurrentProfile(); if(user && ...

Implementing Default Language in Next.js 14 for Static Export without URL Prefix: A Step-by-Step Guide

Currently, I am in the process of developing a website using Next.js 14, with the intention of exporting it as a static site for distribution through a CDN (Cloudflare Pages). The website I am working on requires support for internationalization (i18n) to ...

The request to the External API using a GET method returned a 401 status code along with the message "Authorization token not

As a complete beginner, I am in the process of developing a full-stack app using node.js and react. I have integrated a 3rd party API to scrape social media content and incorporate it into my backend code within the router.get block. Testing this integrati ...

Animating the change in Slider value with Material-UI in React

Recently delving into React, I encountered my first challenge that has been consuming my time for hours. I'm working with a Slider component from Material-UI and seeking to animate the value changes. Currently, when clicking on a button, the slider i ...

How can I define Record values in Typescript based on their specific keys?

I am working on creating a custom data structure that allows me to store values with string keys within the union string | number | boolean: type FilterKey = string; type FilterValue = string | number | boolean; type Filters<K extends FilterKey, T exten ...

How to Keep MUI Draggable Dialog Within Screen Boundaries in React

Currently, I am utilizing the MUI library and focusing on the Draggable Dialog component available here. My main inquiry revolves around how to restrict the dialog from being dragged beyond the screen boundaries. Even after attempting to implement the fo ...