super(props)
this.state = {
id: this.props.match.params.id //此处获取通过path动态参数拼接传过来的信息
}
}
componentDidMount(){
console.log(this.props,‘users props’)
}
render() {
return (
我是users:{this.state.id}
)
}
}
export default Users
获取:this.props.match.params.id
可以打印props,查看里面的内容,不难发现,props中存在该信息
那么对应的编程式跳转为:
<button onClick={() => { this.props.history.push({ pathname: ‘/home/users/999’ }) }}>about
//同样,用this.props.match.params.id进行取值
第二种传参方法为query,通过参数query将信息传递过去
获取:this.props.location.query.text
同样,打印出来看看
对应的编程式跳转为:
<button onClick={() => { this.props.history.push({ pathname: ‘/home/users/999’, query: { text: ‘666’ } }) }}>Users
//同样,获取方式this.props.location.query.text
第三种传参方法为state,通过参数state将信息传递过去,用法与query一致
获取:this.props.location.state.text
同样,打印出来看看
对应的编程式跳转为:
<button onClick={() => { this.props.history.push({ pathname: ‘/home/users/999’, state: { text: ‘666’ } }) }}>Users
//同样,获取方式this.props.location.state.text
**ps:**query跟state用一个重要的区别,那就是在页面跳转之后,重新刷新当前页面,query会消失,而state不会消失,即依然保存在location中。
不妨测试一下,对Users.js页面进行修改,如果query不存在,显示“query已消失”
import React from “react”;
class Users extends React.Component {
constructor(props) {
super(props)
this.state = {
text: this.props.location.query ? this.props.location.query.text : ‘query已消失’
}
}
componentDidMount(){
console.log(this.props,‘users props’)
}
render() {
return (
我是users:{this.state.text}
)
}
}
export default Users
通过跳转,获取数据正常,query存在
重新刷新当前页面,则query消失
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
前端面试题汇总
JavaScript
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
性能
linux
前端资料汇总
频】](https://bbs.csdn.net/topics/618166371)**
性能
linux
前端资料汇总