请参阅代码沙箱上的完整代码。
我的国家/地区 api 应用程序有两个Route组件-
<>
<Header/>
<Router>
<Switch>
<CountriesDataProvider>
<Route path='/' exact component={HomeRoute} />
<Route path='/detail/:countryName' component={DetailsRoute} />
</CountriesDataProvider>
</Switch>
</Router>
</>
当DetailsRoute我点击一个国家/地区时,每个国家/地区的HomeRoute. 但是当直接发出http请求或DetailsRoute刷新时,我看到了这个错误-
/src/comps/Details/DetailsRoute.jsx
无法读取未定义的属性“边界”
它发生在线 -
const promises = country[0].borders.map(fetchNeighborName);
country[0]从中提取countriesData似乎是undefined-
const {countriesData} = React.useContext(CountriesDataContext);
这不是问题BrowserRouter,因为DetailsRoute组件确实呈现但缺少信息。
我不知道当直接 url 请求被发送到组件时是什么逻辑错误导致它不起作用DetailsRoute?请让我知道修复!