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) : 解 ...
随机推荐
- (71)Wangdao.com第十一天_JavaScript 数学对象 Math
Math 对象 封装了数学相关的 属性和方法. 和其他对象不一样,Math 不是一个构造函数,所以不能 new 生成实例, 其所有属性和方法都必须在 Math 对象上调用. 静态属性 Math.PI ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- Oracle 索引 index
索引是一个模式对象,其中包含每个值的条目,该条目出现在表或集群的索引列中,并提供对行的直接快速访问. 创建一个索引: create index 索引名 on 表名 (字段名); 删除索引: dro ...
- 解决Tomcatt下连接数据库的classNoFount问题
在数据库连接单独使用的时候.即作为一个独立类建立在mian方法中,可以正确的使用.例:连接MySql数据库 import java.sql.*; public class SQLtest { // J ...
- Jedis自己整理比较全的API
package com.tebon.ams.utils; import com.alibaba.fastjson.JSON;import com.tebon.ams.util.ObjectUtil;i ...
- Express全系列教程之(四):获取Post参数的两种方式
一.关于POST请求 post方法作为http请求很重要的一部分,几乎所有的网站都有用到它,与get不同,post请求更像是在服务器上做修改操作,它一般用于数据资源的更新.相比于get请求,post所 ...
- 2018-2019-2 网络对抗技术 20165317 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165317 Exp3 免杀原理与实践 实验内容 任务一:正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用 ...
- LeetCode 217 Contains Duplicate 解题报告
题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...
- 2018-2019-2 网络对抗技术 20165236 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165236 Exp3 免杀原理与实践 一.实践内容(3.5分) 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5 ...
- [py]资源搜集
python在线内存图 我会把一些好的py book的资源放到这里 来源: https://piaosanlang.gitbooks.io/spiders/10day/README10.html Le ...