我尝试安排一场比赛以从 url 接收一个名字,但我收到一场比赛 undefinde

I try to lay out a match for receiving a name from url but I receive a match undefinde

我遇到了match undefind这样的错误。我正在上一门关于 React 的旧课程,它做了课程中显示的所有内容,但我得到了一个错误原因。我不明白为什么要匹配undefinde。也许您需要以其他方式接手比赛或以某种方式通过比赛??

import React from "react";
import { BrowserRouter, Route, Routes } from 'react-router-dom'
import { NavBar } from "./components/NavBar";
import { Home } from './pages/Home'
import { About } from './pages/About'
import { Profile } from './pages/Profile'
import { Alert } from "./components/Alert";
import { AlertState } from "./context/alert/AlertState";
import { GithubState } from "./context/github/GithunState";

function App() {
  return (
    <GithubState>
      <AlertState>
        <BrowserRouter>
          <NavBar />
          <div className="container pt-4">
            <Alert alert={{text: 'Test Alert'}} />
            <Routes>
              <Route path="/" element={<Home />} />
              <Route path="/about" element={<About />} />
              <Route path="/profile/:name" element={<Profile />} />
            </Routes>
          </div>
        </BrowserRouter>
      </AlertState>
    </GithubState>
  );
}

export default App;

import React from 'react'
import { useContext, useEffect } from 'react';
import { GithubContext } from '../context/github/githubContex';

export const Profile = ({match}) => {

    // const github = useContext(GithubContext)
    // const name = match.params.name

    // useEffect(() => {
    //     github.getUser()
    //     github.getRepos(name)
    // }, [])



    console.log('asd',match);
    return(
        <div>
            <h1>Profile page</h1>
        </div>
    )
}

import React, {useReducer} from "react"
import axios from 'axios'
import { CLEAR_USERS, GET_REPOS, GET_USER, SEARCH_USERS, SET_LOADING } from "../types"
import { GithubContext } from "./githubContex"
import { githubReducer } from "./githubReducer"

const CLIENT_ID = process.env.REACT_APP_CLIENT_ID
const CLIENT_SECRET = process.env.REACT_APP_CLIENT_SECRET

const withCreads = url => {
    return `${url}client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`
}

export const GithubState = ({children}) => {
    const initialState = {
        user: {},
        users: [],
        loading: false,
        repos: []
    }
    const [state, dispatch] = useReducer(githubReducer, initialState)

    const search = async value => {
        setLoading()

        const response = await axios.get(
           withCreads(`https://api.github.com/search/users?q=${value}&`)
        )

        dispatch({
            type: SEARCH_USERS,
            payload: response.data.items
        })
    }

    const getUser = async name => {
        setLoading()

        const response = await axios.get(
            withCreads(`https://api.github.com/users/users/${name}?`)
        )

        dispatch({
            type: GET_USER,
            payload: response.data
        })
    }

    const getRepos = async name => {
        setLoading()

        const response = await axios.get(
            withCreads(`https://api.github.com/users/users/${name}/repos?per_page=5&`)
        )

        dispatch({
            type: GET_REPOS,
            payload: response.data
        })
    }

    const clearUsers = () => dispatch({type: CLEAR_USERS})
    
    const setLoading = () => dispatch({type: SET_LOADING})

    const {user, users, repos, loading} = state

    return (
        <GithubContext.Provider value={{
            setLoading, search, getUser, getRepos, clearUsers,
            user, users, repos, loading
        }}>
            {children}
        </GithubContext.Provider>
    )
}

link 到 Github https://github.com/Eater228/React-Hooks

检查您的 package.json 文件,如果您使用的是 react-router-dom 的旧版本,请使用最新版本。

match prop 应该从 Route 组件传递下来,它会在您使用 react-router-dom.

时反映正确的数据

更新

您正在使用 element 道具来渲染组件,但那不是正确的道具。您应该将 element 替换为 component,它会起作用。

更新

请考虑使用 useParams 钩子而不是那个 match 道具。 https://reactrouter.com/docs/en/v6/getting-started/overview#reading-url-parameters