react如何监听路由url变化
生活随笔
收集整理的這篇文章主要介紹了
react如何监听路由url变化
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
"componentWillReceiveProps"
"shouldComponentUpdate"
"componentWillUpdate"
"render"
"componentDidUpdate"
更多專業(yè)前端知識,請上 【猿2048】www.mk2048.com 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎
使用這些生命周期鉤子可以監(jiān)聽到路由相同,參數(shù)不同的變化,但是監(jiān)聽不到完全不相同的url的變化。即使路由不同,componentDidMount組件內(nèi)容所更新的東西變了,但是代碼變了,頁面沒有變,找到了一種方法。withRouter
參考:https://reacttraining.com/react-router/web/api/withRouter
import React from 'react' import PropTypes from 'prop-types' import { withRouter } from 'react-router'// A simple component that shows the pathname of the current location class ShowTheLocation extends React.Component {static propTypes = {match: PropTypes.object.isRequired,location: PropTypes.object.isRequired,history: PropTypes.object.isRequired}render() {const { match, location, history } = this.propsreturn (<div>You are now at {location.pathname}</div> )} }export default withRouter(ShowTheLocation) //組件名稱,導(dǎo)出該組件,保證在最外邊?
更多專業(yè)前端知識,請上 【猿2048】www.mk2048.com 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的react如何监听路由url变化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js和css实现手机横竖屏预览思路整理
- 下一篇: 小程序在父组件执行子组件方法,可适用于下