我正在尝试将商店注入我的React组件但是我收到以下错误:
Undefined不是一个函数(评估'装饰器(目标,属性,desc)')
在 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;
}
}
}
答案 0 :(得分:2)
在这种情况下你不需要注射。您将商店直接传递到PoolComponent
,因此不需要它。但是你需要改变一些事情:
在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
中导出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;