I am trying to shift from Redux to Mobx and tried setting up an array in the mobx store, i use decorators to set the value. I am able to return static value from the store but the array length below always throws and undefined value. Not sure what is wrong with the below code. Any suggestions ?
import {observable, action, computed} from 'mobx';
class FormDataStore {
@observable formdata = [1,2,3];
@action updateFormData = (formdata) => {
this.formdata.push(formdata);
}
@computed get readdata() {
return this.formdata.length;
}
}
const store = new FormDataStore();
export default store
Error
FormDataStore.js:12 Uncaught TypeError: Cannot read property 'length' of undefined
at FormDataStore.get (FormDataStore.js:12)
at trackDerivedFunction$$1 (mobx.module.js:1142)
at ComputedValue$$1.computeValue (mobx.module.js:934)
at ComputedValue$$1.trackAndCompute (mobx.module.js:919)
at ComputedValue$$1.get (mobx.module.js:879)
at ObservableObjectAdministration$$1.read (mobx.module.js:3822)
at FormDataStore.get (mobx.module.js:4086)
at FormDataStore.get (mobx.module.js:295)
at PreviewWindow.render (PreviewWindow.js:24)
at Object.allowStateChanges$$1 (mobx.module.js:653)
Below is how i try to read data
import React from "react";
import { Container } from "semantic-ui-react";
import Highlight from "react-highlight";
import {inject,observer} from 'mobx-react';
@inject('FormDataStore')
@observer
class PreviewWindow extends React.Component {
render() {
const {FormDataStore} = this.props;
console.log(FormDataStore)
return (
<Container>
<h1>JSon Preview</h1>
<h1>{FormDataStore.getFormData}</h1>
<Highlight language="javascript">{FormDataStore.readdata}</Highlight>
</Container>
);
}
}
export default PreviewWindow;