reactjs - React Router:从 Api 为 Json 数据创建单个页面
问题描述
我有一个应用程序,它从 api 呈现用户列表,我想要做的是当用户单击列表之一时,它通过 React Router 指向该用户信息页面。在 React 上实现这一点的最佳方法是什么?
我试图解决它,但没有一个好结果。
代码很敏感,但这是我到目前为止所做的图片:索引组件:
- 路由到索引
- 路由到其他页面 路由到单页用户页面:
- API 调用
- 通过链接到其单个页面的用户数据呈现
我可以访问页面但无法传递要呈现的数据
解决方案
使用Link
fromreact-router-dom
通知您的路由器位置已更改,然后让路由器对其进行评估并确定它是它加载的视图/容器。
这是一个工作示例: https ://reacttraining.com/react-router/web/example/basic
以下是一些关于如何使用路由器获取这些详细信息的示例代码:
--- Router 的使用示例并传入它的 props:
<Route
exact
path="/user/view/:id"
render={props => <CustomerView user={this.state.user} {...props} />
}
/>
// By spreading the props into the View you have access to the this.props.match.params.id
--- 在您的用户容器中
fetchUser = () => {
const id = this.props.match.params.id;
postData("/documents/fetch-document", { id: id }).then(res => {
this.setState({
document: res,
...this.defaultState,
isLoading: false
});
});
};
推荐阅读
- python - 您能否根据日期时间值是否为“无”进行条件编码?
- reactjs - .map() 迭代的值不会反映在 html 属性中的函数内部
- r - 如何将列从 tibble 转换为 row.names
- javascript - 拖动后防止点击
- python - 从 docker 容器上运行的烧瓶 api 访问本地目录文件
- javascript - 无法绑定到新的构造函数
- c# - 无法创建实例或抽象类型或接口“按钮”XNA/Mono 游戏
- web-services - 为什么性能测试时“孵化率”很重要?
- c - 将文件中的每一行设置为 char*
- ios - 颤振)iOS 模拟器部署目标“IPHONEOS_DEPLOYMENT_TARGET”设置为 8.0