第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

react-router的withRouter方法是干嘛的,什么時(shí)候要用到?

react-router的withRouter方法是干嘛的,什么時(shí)候要用到?

慕娘7196486 2018-01-11 10:08:52
查看完整描述

1 回答

?
Jokcy

TA貢獻(xiàn)2條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先withRouter可以用來(lái)給組件注入router相關(guān)的一些參數(shù),比如:

```

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.props

? ?return (
? ? ?<div>You are now at {location.pathname}</div>
? ?)
?}
}

// Create a new component that is "connected" (to borrow redux// terminology) to the router.

const ShowTheLocationWithRouter = withRouter(ShowTheLocation)

```

其次withRouter是專門用來(lái)處理數(shù)據(jù)更新問題的。在使用一些redux的的`connect()`或者mobx的`inject()`的組件中,如果依賴于路由的更新要重新渲染,會(huì)出現(xiàn)路由更新了但是組件沒有重新渲染的情況。這是因?yàn)閞edux和mobx的這些連接方法會(huì)修改組件的`shouldComponentUpdate`。

座椅在使用withRouter解決更新問題的時(shí)候,一定要保證withRouter在最外層,比如`withRouter(connect(Component))`

查看完整回答
6 反對(duì) 回復(fù) 2018-01-12
  • 1 回答
  • 0 關(guān)注
  • 11711 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)