React 列表页面传递参数
React 列表进入详情页面
首先安装 react-router-dom (4.0) npm/yarn install react-router-dom
路由跳转配置
列表 父组件 this.props.history.push( { pathname:'/detail', state: data } )
上述的data 为明细的数据
那么详情页面如何接收父组件的数据呢?
const detaildata = this.prop.location.stata.data
注意如果 父组件进入详情页面 this.props.history.push();这个报错时. 引入 import { withRouter } from 'react-router'即可。
部分代码如下
列表组件
import React, { Component } from 'react'
import { withRouter } from 'react-router';
export class List extends Component {
constructor(props) {
super(props);
this.state={
list: [
{
"author": "acemarke",
"points": 125,
"story_text": null,
"comment_text": null,
"num_comments": 32,
"story_id": null,
"story_title": null,
"story_url": null,
"parent_id": null,
"created_at_i": 1460737187,
"relevancy_score": 6666
},
{
"author": "jlongster",
"points": 124,
"story_text": null,
"comment_text": null,
"num_comments": 54,
"story_id": null,
"story_title": null,
"story_url": null,
"parent_id": null,
"created_at_i": 1448479344,
"relevancy_score": 6397
},
{
"author": "myth_drannon",
"points": 123,
"story_text": null,
"comment_text": null,
"num_comments": 78,
"story_id": null,
"story_title": null,
"story_url": null,
"parent_id": null,
"created_at_i": 1499396674,
"relevancy_score": 7526
}]
}
}
viewdetail (item) {
this.props.history.push({ pathname: '/detail', state: {data:item} })
}
render() {
return (
<div>
{ths.state.list.map(item => {
return (
<div key={item.points} onClick={ ()=>this.viewdetail(item)} >
<span>{item.author}</span>
<span>{item.num_comments}</span>
<span>{item.points}</span>
</div>
)
})}
</div>
)
}
}
export default withRouter(List)
详情页面
import React, { Component } from 'react'
export class DetailList extends Component {
constructor(props) {
super(props)
const data = this.props.location.state.data;
this.state={
data:data
}
}
render() {
return (
<div>
<List>
<div>
{this.state.data.author}
</div>
</List>
</div >
)
}
}
export default DetailList
React 列表页面传递参数的更多相关文章
- JSP页面传递参数乱码问题整理
1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...
- 前端 使用localStorage 和 Cookie相结合的方式跨页面传递参数
A页面 html代码: 姓名:<input type="text" id="name1"> 年龄:<input type="text ...
- router-link跳转页面传递参数及页面刷新方法
使用router-link传参: 第一种: 路径:http://localhost:8080/goodListP?id=2 跳转的页面获取参数: this.$route.query.id 第二种: 路 ...
- Jquery Javascript 跳转页面传递参数以及获取url的参数
传递参数: window.location='editCourse.html?dataId='+dataId+''; 获取url中的参数(封装的方法): function getUrlParam ...
- ionic 页面传递参数
1.使用AngularJS自带的$cacheFactory服务 $cacheFactory 从字面直译即为缓存工厂,可以用它来生成缓存对象,缓存对象以key-value的方式进行数据的存储,在整个应用 ...
- jsp页面传递参数是如何与javabean进行关联的
总结:1.severlet容器是通过JavaBean中的属性方法名来获取属性名的,然后根据此属性名来从request中取值 2.JavaBean中属性方法的命名,set后的名称要与你从request中 ...
- SpringMVC 接受页面传递参数
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
随机推荐
- __x__(29)0908第五天__高度塌陷 问题
高度塌陷 在文档流中,父元素的高度默认是被子元素撑开的. 但是当为 子元素 设置 float 时,子元素会完全脱离文档流,无法再撑开父元素,导致父元素高度塌陷...以致于布局混乱 变成 BFC块级格式 ...
- Web Driver 8中定位方法 ——基于python语言
WebDriver提供了八种元素定位方法,在python 语言中,方法如下: id定位:find_element_by_id("id值"):id属性是唯一的. 1 driver ...
- RTMP 摄像头推流至七牛云直播
设置图 报警设置 可以图片和视频保存到服务器,通过 ftp sd 卡数据 http://192.168.1.101/sd/20190127/ rtmp 推流 参考文档 淘宝凯锐视讯RTMP推流说明书 ...
- 实现全选、全不选功能 JQuery
<input type="checkbox" name="" id="checkAllChange" value="&quo ...
- Myeclipse的使用技巧
1.1.MyEclipse自定义注释 一.修改进入路径: Window->Preference->Java->Code Style->Code Template-> ...
- sql server2012远程连接用IP登陆进入设置步骤
第一步:把数据库上要设置的设置好. ↑这步是不是要,我也没弄清楚. 第二部是数据库的配置: 你的电脑图标,右击管理. 以上就是所有步骤. 再次登陆成功.
- 人工智能--AI篇
AI背景 在当今互联网信息高速发展的大背景下,人工智能(AI)已经开始走进了千家万户,逐渐和我们的生活接轨,那具体什么是AI呢? 什么是人工智能(AI)? 人工智能:简单理解就是由人制造出来的,有一定 ...
- VUE组件间数据方法的传递,初步了解
父组件的数据传递到子组件: 子组件:(其中fMsg是要从父组件传递过来的数据,注意fMsg要在子组件props里先定义) 父组件:(使用v-bind,将自身数据绑定给中转属性fMsg,从而通过 子组件 ...
- python之str字符串
字符串是python非常重要的数据类型,它是一个序列(列表和元组也是序列),有下标,可以通过下标遍历字符串序列:同时字符串也是一个不可变数据类型,每次使用"+"拼接字符串时都会产生 ...
- Jmeter学习之--dubbo接口测试
背景:公司的h5和APP都需要调用许多非http的服务,需要对服务的性能和自动化测试 工具:IDEA ,maven,Jmeter 参考文档: https://testerhome.com/topics ...