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 列表页面传递参数的更多相关文章

  1. JSP页面传递参数乱码问题整理

    1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...

  2. 前端 使用localStorage 和 Cookie相结合的方式跨页面传递参数

    A页面 html代码: 姓名:<input type="text" id="name1"> 年龄:<input type="text ...

  3. router-link跳转页面传递参数及页面刷新方法

    使用router-link传参: 第一种: 路径:http://localhost:8080/goodListP?id=2 跳转的页面获取参数: this.$route.query.id 第二种: 路 ...

  4. Jquery Javascript 跳转页面传递参数以及获取url的参数

    传递参数: window.location='editCourse.html?dataId='+dataId+''; 获取url中的参数(封装的方法):    function getUrlParam ...

  5. ionic 页面传递参数

    1.使用AngularJS自带的$cacheFactory服务 $cacheFactory 从字面直译即为缓存工厂,可以用它来生成缓存对象,缓存对象以key-value的方式进行数据的存储,在整个应用 ...

  6. jsp页面传递参数是如何与javabean进行关联的

    总结:1.severlet容器是通过JavaBean中的属性方法名来获取属性名的,然后根据此属性名来从request中取值 2.JavaBean中属性方法的命名,set后的名称要与你从request中 ...

  7. SpringMVC 接受页面传递参数

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  8. Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数

    点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...

  9. SpringMVC 页面传递参数到controller的五种方式

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

随机推荐

  1. C语言复习0_准备工作

    前言: 第一次接触C语言是大一下学期的时候,2013年上半年,那个时候的开发工具还是VS C++,今天了解了一下,常用的开发工具变成了CodeBlocks.决定趁着毕业到入职这一段CD时间,拾起这门语 ...

  2. dhtmlx Gantt知识点2

    <link rel="stylesheet" href="../../codebase/skins/dhtmlxgantt_skyblue.css?v=5.2.0& ...

  3. Enable Coded UI Testing of Your Controls

    http://msdn.microsoft.com/en-us/library/hh552522.aspx AccessibleObject Class http://msdn.microsoft.c ...

  4. Jenkins实现自动运行jmeter脚本

    下载安装包 --jenkins的war包 下载地址:http://jenkins-ci.org/ 链接:https://pan.baidu.com/s/1VhwgYWqn3Bex2kCHigW5wA ...

  5. Js中Map对象的使用

    Js中Map对象的使用 1.定义 键/值对的集合. 2.语法 mapObj = new Map() 3.备注 集合中的键和值可以是任何类型.如果使用现有密钥向集合添加值,则新值会替换旧值. 4.属性 ...

  6. Java-多态经典例子

    public class A { public String show(D obj) { return ("A and D"); } public String show(A ob ...

  7. 现代IM系统中消息推送和存储架构的实现

    现代IM系统中消息推送和存储架构的实现-云栖社区-阿里云 https://yq.aliyun.com/articles/253242

  8. lucene基础

    同一个域中,即使相同的单词,如出现两次JAVA,也是不同的token,但他们对应相同的term,在term中记录这些token信息 数据库数据,与luence数据 需要搜寻(也即索引)的field,存 ...

  9. 如何配置Bitbucket的ssh

    在bitbucket上使用https协议,经常会在提交代码的时候出错,让人很着急上火,但是用ssh就要方便很多.下面介绍一下设置ssh的方法:1.在终端中运行ssh-keygen.2.然后一路ente ...

  10. python基础之 序列化,os,sys,random,hashlib

    1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...