简体   繁体   English

用导航和装饰器反应本机Mobx会引发错误

[英]React native Mobx with navigation and decorator throws an error

When im trying to use React Native Single Store. 当我试图使用React Native Single Store时。 After injecting into Home Component the store is not visible in props of Home component. 注入Home组件后,商店在Home组件的道具中不可见。

By displaying the props of Home component im not able to access the store that was passed to Home component from App.js using Provider. 通过显示Home组件的道具,im无法访问使用Provider从App.js传递给Home组件的商店。

GitHub Link: https://github.com/FreelancerAnkit/Mobx1 GitHub链接: https : //github.com/FreelancerAnkit/Mobx1

Please do help! 请帮忙!

Store.js Store.js


import {observable, action} from 'mobx';

class TestStore {
  @observable loading = true;
  @observable count = 123 ;

  @action loadingCompleted() {
    this.loading = false;
  }

  @action toggleLoading() {
    this.loading = this.loading ? false : true;
  }
}

export default new TestStore();

App.js App.js



import React, {Component} from 'react';
import { Provider, inject, observer  } from "mobx-react/native";
import Home from './app/Home';
import store from "./app/TestStore";

class App extends Component{

  render(){
    return (
      <Provider {...{store: store}}>
        <Home />
      </Provider>
    )
  }
}

export default App;

Home.js Home.js


import {inject, observer} from "mobx-react/native";
import React from "react";
import { ActivityIndicator,View, Text } from "react-native";

@inject('store') @observer
export default class Home extends React.Component {

   componentDidMount() {
     alert(JSON.stringify(this.props))
   }
   render() {
      return (
         <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
            <Text>Landing Page</Text>
         </View>
      );
   }

}

Expected Result: alert this.props should show my store but it does not have my store passed by Provider of Mobx 预期结果:提醒this.props应该显示我的商店,但没有Mobx提供商通过我的商店

it's better to use this 最好用这个

import { Provider } from 'mobx-react/native'

.... ....

<Provider { ...Store }>
    <Login />
  </Provider>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM