mobx-observer-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

mobx-observer-hook

Usage

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(()=><div 
    className="my-component">{model.name} - {model.time.toString()}</div>,[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(
  <MyComponent model={m} />,
  el
);
 
setInterval(()=>{
  m.setTime(new Date());
},1000);
 

Readme

Keywords

none

Package Sidebar

Install

npm i mobx-observer-hook

Weekly Downloads

4

Version

1.0.0

License

ISC

Unpacked Size

3.17 kB

Total Files

6

Last publish

Collaborators

  • suhail