溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在vuex中使用react

發(fā)布時(shí)間:2021-03-26 16:56:46 來源:億速云 閱讀:188 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)如何在vuex中使用react,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

響應(yīng)式數(shù)據(jù)觀測(cè)系統(tǒng)

vue的一大特色就是響應(yīng)式數(shù)據(jù)觀測(cè)系統(tǒng),它可以在get數(shù)據(jù)時(shí)收集依賴,在set數(shù)據(jù)時(shí)觸發(fā)更新。vuex借助于vue的數(shù)據(jù)觀測(cè)系統(tǒng),可以輕松的收集數(shù)據(jù)依賴,并且依賴可以精細(xì)到組件的粒度,也就是說某一狀態(tài)改變時(shí),只有依賴到這一狀態(tài)的組件才會(huì)觸發(fā)rerender,這樣看來redux體系就比較傻,只要提交action,就會(huì)從根組件rerender(react-redux內(nèi)部自動(dòng)進(jìn)行shouldCompoentUpdate判斷)。

如何在vuex中使用react

上圖來自于vue官網(wǎng)對(duì)vuex架構(gòu)的說明,鏈接。

上圖中的component是vue component,只要vue component執(zhí)行render,那么vuex的數(shù)據(jù)響應(yīng)系統(tǒng)就可以自動(dòng)的收集依賴,當(dāng)狀態(tài)改變時(shí),依賴于此狀態(tài)的組件就會(huì)重新渲染。既然我們要實(shí)現(xiàn)的是一個(gè)類vuex的狀態(tài)管理工具,即支持以get的方式收集依賴,以set的方式觸發(fā)更新,所以reux利用了vue的響應(yīng)式數(shù)據(jù)觀測(cè)系統(tǒng),正所謂前人種樹,后人乘涼。

如何收集依賴

我們已經(jīng)有了響應(yīng)式數(shù)據(jù)系統(tǒng),接下來要解決的問題就是如何收集依賴,收集依賴必須要觸發(fā)get,而觸發(fā)get的前提是組件可以拿到store,因此第一步是向組件注入store。類似react-redux,reux提供了Provider使子組件可以拿到store。

class Provider extends Component {
 getChildContext() {
  return {store: this.props.store};
 }

 render() {
  const { children } = this.props;
  return children;
 }
}
Provider.childContextTypes = {
 store: PropTypes.object
};

相應(yīng)的子組件可以context拿到store,如下

class Child extends Component {
 render() {
  // store => this.context.store
 }
}
Child.contextTypes = {
 store: PropTypes.object
};

這樣寫的缺點(diǎn)顯而易見,每個(gè)子組件都需要定義contextTypes,同樣的類似于react-redux,reux提供了connect函數(shù),用于映射state => props

const connect = (mapStateToProps = () => {}) => {
 return (WrappedComponent) => {
  const Wrapper = class extends Component {
   render() {
    const store = this.context.store;
    const props = Object.assign({}, this.props, mapStateToProps(store.state, this.props), {dispatch: store.dispatch, commit: store.commit});
    return <WrappedComponent {...props} />
   }
  }
  Wrapper.contextTypes = {
   store: PropTypes.object
  };
  reaturn Wrapper;
 }
}

這樣一來,只要組件執(zhí)行render方法,便會(huì)觸發(fā)get鉤子,從而使得store自動(dòng)收集依賴,我們?cè)傧胂乱蕾囀鞘裁?,其?shí)依賴應(yīng)該是組件實(shí)例,那么當(dāng)set鉤子觸發(fā)時(shí),每個(gè)依賴(即組件實(shí)例)只要執(zhí)行forceUpdate方法就可以達(dá)到rerender的效果。

但是問題是,get鉤子觸發(fā)時(shí),如何確定依賴到底是誰呢?借鑒vue,我們定義一個(gè)stack,當(dāng)componentWillMount時(shí)進(jìn)棧,當(dāng)componentDidMount時(shí)出棧

componentWillMount() {
 pushTarget(this);
}

componentDidMount() {
 popTarget(this);
}

這樣當(dāng)get鉤子觸發(fā)時(shí),當(dāng)前target就是目標(biāo)依賴。同時(shí)應(yīng)當(dāng)注意,當(dāng)組件update時(shí)應(yīng)當(dāng)重新收集依賴,因?yàn)閡pdate之后依賴關(guān)系很可能已經(jīng)變化了

update() {
 // 清空依賴
 this.clear();
 pushTarget(this);
 this.forceUpdate(() => {
  popTarget(this);
 })
}

以上就是如何在vuex中使用react,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI