Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mobx-react 'observer' without decorator syntax?

I am trying to shoe horn mobx into a vr application I am making with react 360. I've tried to use the decorator syntax but after wasting a solid chunk of time trying to implement it, I've decided to use the nondecorator syntax. Here is an example that I came across from the mobx documentation that I have a question on. Here is the code:

import {observer} from "mobx-react";

var timerData = observable({
    secondsPassed: 0
});

setInterval(() => {
    timerData.secondsPassed++;
}, 1000);

@observer class Timer extends React.Component {
    render() {
        return (<span>Seconds passed: { this.props.timerData.secondsPassed } </span> )
    }
};

ReactDOM.render(<Timer timerData={timerData} />, document.body);

Notice the observer declaration on the Timer class. The documentation states this.

Note that using @observer as decorator is optional, observer(class Timer ... { }) achieves exactly the same.

Would this be the correct way of implementing Timer?

observer(class Timer extends React.Component {
  render() {
    return (<span>Seconds passed: { this.props.timerData.secondsPassed } </span> )
  }
}) 
like image 833
Dan Rubio Avatar asked Dec 20 '18 23:12

Dan Rubio


People also ask

What is observer in MobX React?

The observer function / decorator can be used to turn ReactJS components into reactive components. It wraps the component's render function in mobx. autorun to make sure that any data that is used during the rendering of a component forces a re-rendering upon change.

How does MobX observable work?

MobX reacts to any existing observable property that is read during the execution of a tracked function. "reading" is dereferencing an object's property, which can be done through "dotting into" it (eg. user.name ) or using the bracket notation (eg. user['name'] , todos[3] ) or destructuring (eg.

How do I enable decorators?

How Do I Enable Decorators in TypeScript? As of writing this guide, decorators are still an experimental feature in TypeScript. To enable this feature, set the experimentalDecorators compiler flag either on the command line or in your tsconfig. json file.


1 Answers

Concerning the code snippet you have added I don't know whether it is a valid way or not, but here's the way I'm using MobX without the decorator syntax in my application project:

Create your MobX store, say MyStore.js like the following:

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

export default class MyStore {
    storeMap = observable(new Map());
    storeArray = observable([]);
    storeBoolean = false

    get storeMapSize() {
        return this.storeMap.size;
    }

    setStoreBoolean(value) {
        this.storeBoolean = value;
    }
}

decorate(MyStore, {
    storeMap: observable,
    storeArray: observable,
    storeBoolean: observable

    storeMapSize: computed,

    setStoreBoolean: action
});

Then in your component Timer.js:

import {inject, observer} from "mobx-react";

class Timer extends React.Component {
    render() {
        return (<span>value from store: { this.props.myStore.storeMap.get('any_key') } </span> )
    }
}

export default inject('myStore')(observer(Timer));

and you can create as many stores as you want and inject all of them to your components using the same inject method and use them in the same way via this.props, for example

export default inject('myStore', 'anotherStore', 'anotherNewStore')(observer(Timer));

like image 155
Mohamed Elsayed Avatar answered Sep 28 '22 02:09

Mohamed Elsayed