I have a problem with two .js files in a react app! I am using the react-router-dom. My problem is with the Link tag

Stanciu Mihai

I'm making a static web app with React. It has more pages so I'm using the react-router-dom, but when i use the Link tag it shows up not just on the page I want it on but also on the page it's suppose to redirect. Except for that everything works just fine: the page it's pointing at is rendering just fine, but with the link in the bottom left corner. Can anyone look over my code and tell me what I am doing wrong?

I only want the link 'To Do List' to show up here:

import React, { Component } from 'react'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import { Link } from 'react-router-dom'
import Todo from '../apps/todo/Todo'

class Projects extends Component {
  render() {
    return (
      <Router>
        <div>
          <Route exact path="/todo" component={Todo} />
          <Link style={projectsTodo} href="/todo">
            To Do List
          </Link>
        </div>
      </Router>
    )
  }
}

The link shows up here:

import React, { Component } from 'react'
import Header from './layout/Header'
import Todos from './Todos'
import uuid from 'uuid'

class Todo extends Component {
  state = {
    todos: [
      {
        id: uuid.v4(),
        title: 'Learn React',
        completed: false,
      },
      {
        id: uuid.v4(),
        title: 'Find suitable web hosting service',
        completed: false,
      },
    ],
  }

  delTodo = id => {
    this.setState({
      todos: [...this.state.todos.filter(todo => todo.id !== id)],
    })
  }

  render() {
    return (
      <div>
        <Header />
        <Todos todos={this.state.todos} delTodo={this.delTodo} />
      </div>
    )
  }
}

export default Todo
mehamasum

This is happening because you are rendering the Link without any specified route (so it is rendered on all routes). See the first example of React Router to get a better understanding.

If you don't want this to happen, then define another component Home (for example) that will hold the Link and add another Route to your Router that will render that component.

import React, { Component } from 'react'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import { Link } from 'react-router-dom'
import Todo from '../apps/todo/Todo'

const Home = props => {
  return (
    <div>
      <h2>Home</h2>
      <Link style={projectsTodo} href="/todo">
        To Do List
      </Link>
    </div>
  )
}

class Projects extends Component {
  render() {
    return (
      <Router>
        <div>
          <Route exact path="/" component={Home} />
          <Route exact path="/todo" component={Todo} />
        </div>
      </Router>
    )
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have a problem with React Router Dom V6 and useState

I am facing a problem in Pagination using states in React JS

I am facing problem using Simple React Lightbox Next js?

I have a problem with my version of React

How do I select Link tag in React Router Dom with CSS?

React Router Dom Problem on Build, im always getting my main page. I already tried with exact

Problem in Link and to in react and react router

How do I make my React App secure? As of now I can login using React DOM Tools, what am I missing?

I am trying to add a router to my react app

React App with React-Router-DOM: Link vs Achor Tag

I have problem with read promise from async function in my react app with

React Todo List app I have a problem implementing the delete functionality

How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a>

I am trying to use redux in my project and I am have a problem with call a function that using dispatch

I have a problem with form validation in JS DOM

Routing problem in React-JS / react-router-dom

Is there a problem with my code or the API I am using?

How can I fix the problem react Switch Autocomplete import from react-router-dom

I am passing the props to a functional component via Link (react-router-dom) but it says the state is 'undefined'

I am not able to use the router in react js

I am trying to open the React Native application but I have this problem What is the solution?

I have a problem with react navigation auth flow

I have a There is a problem with implementing the model window in the react

I want to split my web app into 2 parts using react router. I am using a router inside of one of the previously mentioned routes

Error with the tag Link, react-router-dom

How do I modify part of the DOM outside of my Vue.js app such as a <link> tag?

I should solve a React exercise of a timer and reset it by a button.I have problem with resetting my timer

unable to redirect with this command navigate('/login?redirect=shipping') on react router dom i am using version 6

I am having problem while using filter in react Hooks,how can i solve this issue?