Whenever I initiate react-router to navigate to a different page, React does not render correctly

I am currently working on creating a new List page using the news.json file that I have created.

Below, you can see that I am trying to create a list with a map function and also generating Link hrefs with ${item.id}.

The items are representing news1, news2, news3, etc. and for each one, I have created new1.js, new2.js files to link them together.

However, when I attempted to render it, nothing appeared on the screen. I believe there may be an issue with the rendering process.

Even after searching on Google, I came across a post on Stack Overflow which mentioned something about routers needing a key parameter, but I am still struggling to understand it.

Can someone help me figure out how to modify my code?

const NewWrap =()=>{
  const articleWrapper = news.map((item)=>{
    return(
      <Li key={item.id}>
        
        <img src={item.img} alt="img" style={{width:"260px", height:"200px", padding:"1rem"}}/>
        **<Link to={`/${item.id}`}** style={{textDecoration:"none", color:"black"}}>
          <div style={{padding:"1rem", textAlign:"left", cursor:"pointer"}}>
            <p>{item.title}</p>
            <p>{item.content.substring(0,305)}...</p>
            <p>{item.date}</p>
          </div>
        </Link>
      </Li>
      
      )
    });
    return(
      <Ul>
      {articleWrapper}
    </Ul>
  )
}

const News = () => {
  
  return (
    <div>
     
      <ImgBox>
        <Img src ="../img/bg_mian_01.png"/>
        <ImgText>News and Partners</ImgText>
      </ImgBox>
      
      <ConBox>
        <Subtitle>NEWS</Subtitle> 
        <NewWrap/>

      </ConBox>
      <Switch>
        <Route path="/new1" component={New1}/>
      </Switch>
    </div>
  
  );
};

export default News;

Answer №1

The most recent update to React Router (v6) eliminates the need for a Switch component and requires specifying an element instead of a component.

import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';

<Router>
  <Routes>
    <Route path="/new1" element={<New1 />} />
  </Routes>
</Router>

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

When utilizing a script to deliver a true or false response for jQuery's .load() function

Currently, I have found myself delving deep into jquery to manage the ajax requests in my web applications. Specifically, I am working on a bidding system in PHP that heavily relies on mod_rewrite. Using jQuery with a confirm dialog, I send an Ajax request ...

Step-by-step guide to selecting a specific point on an HTML5 canvas using Python's selenium webdriver

Looking to automate interactions with a simple HTML5 application on a website using Selenium webdriver in Python with Firefox on Linux. The challenge is clicking a button on an HTML5 canvas, then dragging one or two objects around the canvas post-button cl ...

Ways to enable users to add or modify spry widgets

Is there a way to make it easier for users to edit spry accordions/tabbed panels in a navigation tool for locating points in PDFs? The PDFs are updated regularly, so the navigation will need frequent updates. The users who will be maintaining this tool h ...

Struggling to dynamically fill the table with Ajax Json data

After retrieving data from my webservice call, I am sending this JSON response: [ [ { "id": 123, "vendorName": "PoppyCounter", "item": "Chocolate" }, { "id": 1234, "ve ...

PHP function - retrieving the beginning element in the array that is returned for every row

My current database setup involves a MySQL DB with approximately 100 rows. Within each row, there is a list of items stored in a column within the table. The challenge I am facing is extracting the first item from this list in each row. For example, if the ...

Unable to view flash elements in HTML using Django framework

Apologies for any errors in my English, I will do my best to explain clearly. I am new to working with Django and I have a html page with flash content called map.html. I would like to include this page into another page called soporte.html (which extends ...

Populate an array using a callback function within an async.series operation

I have a callback function within 'async.series' that generates multiple values and creates various outputs from 'elements'. Is there a way to save these return values into an array using 'forEach'? async.series( { ...

Top strategies for managing fixed datasets

Imagine having a dataset containing country names and their corresponding phone prefixes, like so: var countryPhonePrefixes = [ { 'name': 'Germany', 'prefix': '+49' }, { 'nam ...

Angular File Sorting Error in Gulp detected

After including .pipe(angularFilesort()) in my gulp script, the task runs wiredep but never proceeds to the default task. It just stops after executing wiredep. However, if I remove .pipe(angularFilesort()), the script works perfectly fine. Can someone h ...

Exploring the power of wild card searches with Json arrays in Couchbase N1QL

Is there a way to perform a wildcard search on a JSON String Array similar to using %Mr.% in N1QL? I am aware that exact values can be queried like 'Jon' in names where names is the JSON array. { "use" : "official" "names" : ["Jon", "Sno ...

Traversing through JSON data in Flutter using iterable

Hey everyone, I'm facing an issue that I need help with. I recently made a successful get request through an API using Flutter. The problem I have is related to a 'mentee id' list that I received from the previous screen. Here is what it loo ...

AngularJS Textarea with New Lines after Comma

As a beginner in front-end development, I am exploring different approaches to handling a text area that inserts a new line after each comma. My main focus is on finding the best solution within AngularJs (filter, Regex, etc). Before: hello,how,are,you ...

Capture a section of the body background to incorporate into the canvas space

As a newcomer to the world of canvas, I have been learning from various sources about how it works. However, my goal is more dynamic and unique than what I've seen so far. I am looking to create a body background for my webpage that is responsive, cen ...

The Server Discovery And Monitoring engine has been marked as obsolete

Currently, I am integrating Mongoose into my Node.js application with the following configuration: mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false }).then ...

Converting a dataframe to json format will produce a single-row output

After exporting a dataframe shaped 1000x55 to Json using the command: full_dataset_df.to_json('fulldataset.json') Upon attempting to load the json file in another notebook using read_json, I noticed that all the data appeared in a single row wi ...

Unable to set an onclick function within a customized dojo widget

I have a custom widget that I've defined as shown below: dojo.declare('myWidget', [dijit._WidgetBase, dijit._Templated], { 'templateString':'<span>' + '<a dojoAttachPoint="linkNode" href="b ...

Toggle Jquery feature will dynamically add the "required" attribute to the input field when the specified div is visible, and it will automatically remove the attribute when the div

I am new to using jQuery and I am facing an issue with my code. I want to make a checkbox act as a toggle with jQuery. When the checkbox is clicked and the toggle displays the div, I want to add the required attribute to the checkbox input. Similarly, when ...

Passing the value of an Angular component to a different component

I have a menu in my application that uses IDs to route content, and I also have a detailed view where the content should be displayed based on those same IDs. Currently, I am trying to display objects by their ID when a button is clicked. However, I' ...

typescript throwing an unexpected import/export token error

I'm currently exploring TypeScript for the first time and I find myself puzzled by the import/export mechanisms that differ from what I'm used to with ES6. Here is an interface I'm attempting to export in a file named transformedRowInterfac ...

When using Vuepress behind a Remote App Server, pages containing iframes may return a 404 error

Creating a static website using Vuepress was a breeze, especially since I included a dedicated section for embedded Tableau dashboards. The website functioned perfectly when accessed online without any issues, displaying the Tableau dashboards flawlessly. ...