日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

react-native中使用mobox数据共享

發布時間:2023/12/13 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 react-native中使用mobox数据共享 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安裝依賴

yarn add mobox

yarn add mobox-react

使用

在組件A中引入mobox-react

inject的作用是把公共的數據store注入到當前組件A中,在組件A中通過this.props.store可以獲取公共數據

observer的作用是監聽store中的數據是否改變,一旦公共數據store改變了就去重新渲染組件

import {inject ,observer} from 'mobox-react'

import { inject, observer } from 'mobx-react'
nterface Props {
    store?: any
}
interface State {
    selectedTab: string
}

@inject('store')
@observer
class Index extends Component<Props, State> {
  state = {
    selectedTab: 'cookbook'
  }
  //setState調用就會去觸發@observer方法讓組件重新渲染
  <View onPress={() => this.setState({ selectedTab: 'cookbook' })}></View>
    //使用本組件的state就是是this.state,使用store中的state就是this.props.state
    <TabNavigator.Item selected={this.state.selectedTab === 'menu'}></TabNavigator.Item>
    {
          this.props.store.isShow ?<View>調用的是公共store中的數據</View>:null
    }

在公共數據存儲store/index.js文件中

import { observable, action, computed } from 'mobx'
import { get } from '@/src/utils/http'

class AppStore {
  @observable list = []

  @observable isShow = true

  @computed
  get swiper() {

  }

  @action
  setList(data) {
    this.list = data;
  }

  @action
  async getList() {
    let result = await get('http://192.168.2.1:8088/hotlist')
    this.setList(result)
  }

  @action
  setVisible(status) {
    this.isShow = status;
  }

}

export default new AppStore()

在App.js中使用

import store from '@/src/store/index'
import { Provider } from 'mobx-react'
export default function App() {
  return (
      <Provider store={store}>
       ...
      </Provider>

  );
}

總結

以上是生活随笔為你收集整理的react-native中使用mobox数据共享的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。