本文介绍了在React组件中注入Store会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的React Component注入商店,但是出现以下错误:

I am trying inject a store to my React Component however I am getting the following error:

在我的 App.js 中,我拥有:

import React, { Component } from 'react';
import PoolComponent from './app/Components/PoolComponent';
import MeasurementsStore from './app/Stores/MeasurementsStore';

export default class PoolApp extends Component {

render() {
    return (
      <PoolComponent store="MeasurementsStore"/>
    );
}

}

在我的 PoolComponent.js

import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { AppRegistry, Text, View, TextInput , Picker, Button} from 'react-native';

@observer
export default class PoolComponent extends Component {
saveItems() {
    console.log('Pressed save');
}
render() {
    const store = this.props.store;
    return (
        <View>
            <Text>Selecteer Pool</Text>
            <Picker>
                <Picker.Item label="Big" value="big"/>
                <Picker.Item label="Small" value="small"/>
            </Picker>
            <Text>PH</Text>
            <TextInput/>

            <Text>Totaal Chloor</Text>
            <TextInput/>

            <Text>Vrij Chloor</Text>
            <TextInput/>
            <Button
                title="Learn More"
                color="#841584"
                accessibilityLabel="Opslaan"
                onPress={this.saveItems}
            />
        </View>
    );
}
}

MeasurementsStore.js 中,我有

import {observable, action, computed} from 'mobx-react';

export default class MeasurementsStore {
    @observable phValue = 0;
    @observable freeChlorine = 0;
    @observable totalChlorine = 0;
    @observable totalAlkalinity = 0;

    @action data(data: Object) {
        if (data.phValue) {
            this.phValue = data.phValue;
        }
        if (data.freeChlorine) {
            this.freeChlorine = data.freeChlorine;
        }
        if (data.totalChlorine) {
            this.totalChlorine = data.totalChlorine;
        }
        if (data.totalAlkalinity) {
            this.totalAlkalinity = data.totalAlkalinity;
        }
    }
}

推荐答案

在这种情况下,您不需要注入.您将商店直接传递到PoolComponent,因此不需要它.但是,您需要更改一些内容:

You don't need inject in this case. You are passing the store directly to your PoolComponent, so there is no need for it. You need to change a few things however:

App.js中传递实际的商店,而不仅仅是将商店名称作为字符串传递:

Pass the actual store, not just the store name as a string, in App.js:

import React, { Component } from 'react';
import PoolComponent from './app/Components/PoolComponent';
import measurementsStore from './app/Stores/MeasurementsStore';

export default class PoolApp extends Component {

render() {
    return (
      <PoolComponent store={measurementsStore}/>
    );
}

mobx导入并在MeasurementsStore.js中导出MeasurementsStore的实例:

Import from mobx and export an instance of MeasurementsStore in MeasurementsStore.js:

import {observable, action, computed} from 'mobx';

class MeasurementsStore {
    @observable phValue = 0;
    @observable freeChlorine = 0;
    @observable totalChlorine = 0;
    @observable totalAlkalinity = 0;

    @action data(data: Object) {
        if (data.phValue) {
            this.phValue = data.phValue;
        }
        if (data.freeChlorine) {
            this.freeChlorine = data.freeChlorine;
        }
        if (data.totalChlorine) {
            this.totalChlorine = data.totalChlorine;
        }
        if (data.totalAlkalinity) {
            this.totalAlkalinity = data.totalAlkalinity;
        }
    }
}

const measurementsStore = new MeasurementsStore();

export default measurementsStore;

这篇关于在React组件中注入Store会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:12