An open API service indexing awesome lists of open source software.

https://github.com/suhdev/mobx-observer-hook


https://github.com/suhdev/mobx-observer-hook

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

# mobx-observer-hook

## Usage

```typescript
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { observable, action } from 'mobx';
import { useObserver } from 'mobx-observer-hook';

class Model {
@observable name = 'test';
@observable time:Date = new Date();

@action.bound
setTime(time:Date) {
this.time = time;
}
}

function MyComponent({model}) {
return useObserver(()=>

{model.name} - {model.time.toString()}
,[model]);
}

let el = document.getElementById('app');

if (!el){
el = document.createElement('div');
el.id = 'app';
document.body.appendChild(el);
}

const m = new Model();
m.name = 'Suhail';

ReactDOM.render(
,
el
);

setInterval(()=>{
m.setTime(new Date());
},1000);

```