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. BOM 浏览器对象模型_渲染引擎_JavaScript 引擎_网页加载流程

    1. 浏览器核心的两个组成部分 渲染引擎 将网页代码渲染为用户视觉可以感知的平面文档 分类: Firefox        Gecko 引擎 Safari        WebKit 引擎 Chrom ...

  2. Windows中使用ssh利用公钥登入远程服务器

      方式:使用 Winscp 密钥登录   我们平时开发多会使用 ftp 来上传下载文件,尤其是很多 Linux 环境下.   其实 Linux 默认是不提供 ftp 的,需要你额外安装 FTP 服务 ...

  3. 《javascript设计模式与开发实践》--- (单一职责原则)

    看的这本书叫<JavaScript设计模式与开发实践> 先规划一下看书的顺序,基础知识我已经大概的浏览了一遍了,没有留下笔记,以后有时间还会补上.本来打算顺着看的.但是我感觉我很难短时间内 ...

  4. 新装的centos怎样显示中文界面

    默认的显示英文界面,即使各种配置中都选择的chinese也没用,默认显示的还是英文. 要在终端输入 vim ~/.bashrc 编辑本用户配置文件 打开后最后一行加入 export LANG=&quo ...

  5. css----transition的应用,产生动画效果。

    应用transition属性产生动画效果 css中的transition属性设置元素的变化过程所需的时间. <!DOCTYPE html> <html> <head> ...

  6. 关于反射和JVM的整理

  7. ide phpStorm更换主题

    #主题下载地址 http://www.phpstorm-themes.com #更换方式 1.将主题配置保存在 xxx.icls(如果是xml也保存成.icls) 2.打开phpStorm设置(中上方 ...

  8. laravel读取excel

    $filePath = 'storage/exports/成员信息.xls'; Excel::load($filePath, function ($reader) {// $data = $reade ...

  9. 一种JNI混淆方案

    转载自:http://www.liuling123.com/2016/06/so_method_mix.html 感谢原作者 侵删 默认情况下,使用JNI时与native对应的JNI函数名都是Java ...

  10. JSTL将number类型转化为String类型

    <c:set var="lm1"> <c:out value="${lm}" /> </c:set>