mobx在react hooks中的應(yīng)用方式
mobx在react hooks的應(yīng)用
hooks 實(shí)現(xiàn)inject功能
// utils/useStores.js import React from 'react' import { MobXProviderContext} from 'mobx-react'; import RootStore from '~/stores/index' const useStores = (name) => React.useContext(MobXProviderContext)[name] export default useStores
局部的store
// store.js import { observable, action } from 'mobx'; class Store { @observable userInfo = { name: 'xsx' }; @observable count = 11; @action handleClick() { this.count +=1; } } export default Store;
頁面中使用
import React, { ReactElement } from 'react'; import Store from './store.ts'; import useStores from '~/utils/useStores'; import { useObserver, useObservable } from 'mobx-react-lite'; interface Props { [props: string]: any; } export default function index(props: any): ReactElement { // 手動(dòng)傳入字符串,選擇要使用的內(nèi)容 該處為全局store的引用,與之前的class下的@inject('userStore')同理 const globalStore = useStores('userStore'); // 添加觀察者模式 該處為局部的store 與之前class下的 let myStore = new Store(); 同理 let myStore = useObservable(new Store()); return useObserver(() => ( <div> {myStore.count} ==================== {store.count} <button onClick={() => { myStore.handleClick(); globalStore .handleClick(); }} > add </button> </div> )); }
mobx與react hooks配合使用
import React from 'react'; import {useObserver, Observer, observer} from 'mobx-react'; import {appStore} from './appStore'; // 方法1: function Methods1() { return useObserver(() => ( <div onClick={() => {appStore.count = 1}}>{appStore.count}</div> )) } // 方法2(Observer一定要用函數(shù)): function Methods2 () { return ( <Observer> {() => <div onClick={() => {appStore.count = 1}}>{appStore.count}</div>} </Observer> ) } // 方法3(推薦): export default observer(props => { return <div onClick={() => {appStore.count = 1}}>{appStore.count}</div> })
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
react-three-fiber實(shí)現(xiàn)炫酷3D粒子效果首頁
這篇文章主要介紹了react-three-fiber實(shí)現(xiàn)3D粒子效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08在React項(xiàng)目中實(shí)現(xiàn)一個(gè)簡單的錨點(diǎn)目錄定位
錨點(diǎn)目錄定位功能在長頁面和文檔類網(wǎng)站中非常常見,它可以讓用戶快速定位到頁面中的某個(gè)章節(jié),本文講給大家介紹一下React項(xiàng)目中如何實(shí)現(xiàn)一個(gè)簡單的錨點(diǎn)目錄定位,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-09-09react清空ant.design中表單內(nèi)容的方法實(shí)現(xiàn)
本文主要介紹了react清空ant.design中表單內(nèi)容的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12React Native使用fetch實(shí)現(xiàn)圖片上傳的示例代碼
本篇文章主要介紹了React Native使用fetch實(shí)現(xiàn)圖片上傳的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03react+zarm實(shí)現(xiàn)底部導(dǎo)航欄的示例代碼
本文主要介紹了react?+?zarm?實(shí)現(xiàn)底部導(dǎo)航欄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05詳解使用create-react-app快速構(gòu)建React開發(fā)環(huán)境
這篇文章主要介紹了詳解使用create-react-app快速構(gòu)建React開發(fā)環(huán)境,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05React實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件
本文主要介紹了React實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07