React Router throws error with certain component

motionlesseuphoric

I would like to implement routing in my project but I keep getting the same error message and can't figure out how to solve the problem. I want to start with the MappedCharacters component and assign it to the "/" route. Here I get the following error message: "Uncaught Error: [MappedCharacters] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>".

I have also set the browser router in Index.js (so that can't be the problem). As soon as I remove the MappedCharacters component it works. So how could I wrap MappedCharacters to make routing work?

App.js

import "./App.css";
import Header from "./components/Header";
/* import styled from "styled-components"; */
import Navbar from "./components/Navbar";
import { useState, useEffect } from "react";
import MappedCharacters from "./components/MappedCharacters";
import AddFavorites from "./components/AddFavorites";
import SearchBox from "./components/Searchbox";
import { Routes, Route, useNavigate } from "react-router-dom";

const URL = 'https://rickandmortyapi.com/api/character'; /* Das hier ist der API Link in einer Variablen */

function App() {
  const [characters, setCharacters] = useState([]);
  const [hasError, setHasError] = useState(false)
  const [favorites, setFavorites] = useState([])
  const [searchValue, setSearchValue] = useState([])

  /* Hier folgend wird von der API gefetched */
  async function fetchCharacters() {
    try {
      const response = await fetch(URL);
      const result = await response.json() 
      setCharacters(result.results);
      setHasError(false);
    }
    catch(error) {
    setHasError(true);
    console.error(error);
    }
  }
 /*  Ohne den useEffect klappt das fetchen und rendern der Cards nicht!!! */
  useEffect(() => { 
    fetchCharacters();
  }, []);

  const addFavoriteCharacter = (character) => {
    const newFavoriteList = [...favorites, character];
    setFavorites(newFavoriteList);
  }

  return (
    <div className="App">
      <Header />
     
      <SearchBox searchValue={searchValue} setSearchValue={setSearchValue} />
      <Routes>
        <Route path="/">
          {characters.map((character) => (
            <MappedCharacters
              name={character.name}
              gender={character.gender}
              status={character.status}
              species={character.species}
              image={character.image}
              favoriteComponent={AddFavorites}
              handleFavoritesClick={addFavoriteCharacter}
            />
          ))}
        </Route>
      </Routes>
      <Navbar />
    </div>
  )
}

export default App;

/* const CardElements = styled.li`
color: whitesmoke;
list-style: none;
`; */

index.js

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

reportWebVitals();

MappedCharacters.js

import React from 'react'

const MappedCharacters = (props) => {
  const FavoriteComponent = props.favoriteComponent;
  return (
    <div>
      <li key={props.id} >
        <h1>{props.name}</h1>
        <p>Gender: {props.gender}</p>
        <p>Status: {props.status}</p>
        <p>Species: {props.species}</p>
        <div>
          <img src={props.image} alt="img"></img>
        </div>
        <FavoriteComponent />
      </li>
    </div>
  )
}

export default MappedCharacters
Drew Reese

Issue

The issue is with this route:

<Route path="/">
  {characters.map((character) => (
    <MappedCharacters
      name={character.name}
      gender={character.gender}
      status={character.status}
      species={character.species}
      image={character.image}
      favoriteComponent={AddFavorites}
      handleFavoritesClick={addFavoriteCharacter}
    />
  ))}
</Route>

As the error indicates, only other Route or React.Fragment components are valid children of the Route component. Match routed content should be rendered on the Route component's element prop.

Solution

Move the characters array mapping into the element prop.

<Route
  path="/"
  element={(
    <>
      {characters.map((character) => (
        <MappedCharacters
          key={character.id} // <-- don't forget valid React key!
          name={character.name}
          gender={character.gender}
          status={character.status}
          species={character.species}
          image={character.image}
          favoriteComponent={AddFavorites}
          handleFavoritesClick={addFavoriteCharacter}
        />
      ))}
    </>
  )}
/>

Or abstract the mapping into a React component and pass the characters array as a prop.

const Characters = ({ addFavoriteCharacter, characters }) => (
  <>
    {characters.map((character) => (
      <MappedCharacters
        key={character.id} // <-- don't forget valid React key!
        name={character.name}
        gender={character.gender}
        status={character.status}
        species={character.species}
        image={character.image}
        favoriteComponent={AddFavorites}
        handleFavoritesClick={addFavoriteCharacter}
      />
    ))}
  </>
);

...

<Route
  path="/"
  element={<Characters {...{ addFavoriteCharacter, characters }} />}
/>

Edit react-router-throws-error-with-certain-component

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Router URL Throws Error With Parameter

Typescript & React - Render a component throws an error

Async throws error in React Functional Component

Enzyme Throws an error if the React Component requires jQuery

Formik hooks "useField" throws error stating that the component is not a React function component

Render component on certain paths only react router v6

Using multiple react router switch throws 404 error

React | Functional component throws error useState Object(...) is not a function

high order component throws an invalid react element error (reactjs)

Mounting React-Popup component throws render error

React component throws a const undefined error after refreshing

React Js: Pass props to React Router's Link component Error

(react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component

react router lazy component

Stateless component React router

React router is not mounting the component

React router not rendering component

React router not displayed a component

Redirection with React Router Component

React - Change component by the router

React router not rendering component

React Router - "ambiguous indirect export: Switch" error in App component

React-router 6 errorElement not rendering an error component

react navigation send me this error: the component for route router must be a react component

Reactstrap Card component throws error

Error: data.map is not a function. This error throws whenever I run react component

React Router V6 - Error: useRoutes() may be used only in the context of a <Router> component

React router error "useRoutes() may be used only in the contect of a <Router> component."

Why does react-router treat a regex `path` correctly, but throws an error at the same time?