reactjs - React Route Path 显示空白页面
问题描述
我正在尝试在 Main 中获取 Singleshow 并在指定路径中显示文本
单秀 index.js
import React, { Component } from 'react';
class SingleShow extends Component {
render () {
return (
<div>
<p>Single Shows</p>
</div>
)
}
}
export default SingleShow;
主索引.js
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Shows from '../Shows';
import SingleShow from '../SingleShow';
const Main = props => (
<Switch>
<Route exact path="/" component={Shows} />
<Route path="/shows/:id" component={SingleShow} />
</Switch>
);
export default Main;
路由确切路径完美运行,但是当我http://localhost:3000/shows/123
在浏览器中输入时,我得到一个空页面。
当我在 Chrome 中检查时,我只看到两件事:
[HMR] 等待来自 WDS 的更新信号...
在 Startnull
解决方案
您应该在 SingleShow 组件中使用构造函数
constructor(props) {
super(props);
// Don't call this.setState() here!
this.state = { id:this.props.match.params.id};
}
然后在渲染
render () {
return (
<div>
<p>{this.state.id}</p>
</div>
)
}
推荐阅读
- clang - 从位码文件创建 llvm::module 对象
- r - 合并具有不同列的数据框
- python - 从 Cloud Function 到 App Engine 的请求返回错误 401
- javascript - Ajax 分页不记得上一页
- node.js - 带有 dotnet 和节点图像的 GitLab 管道
- c++ - 您是否需要知道在 C++ 中处理它们会发生什么异常?
- reactjs - 基本 URL 的 React Router 通配符问题
- mysql - 获取两个不同领域的空缺总和
- cmake - 未找到 add_custom_command 的 CMake 输出
- c# - 根据文件类型将文本文件写入 ftproot 文件夹时访问被拒绝错误