0.2.4 • Published 4 years ago

mobx-react-bind v0.2.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Installation

npm install mobx-react-bind

or

yarn add mobx-react-bind

Usage

Basic Usage

Define a mobx store

import { observable, action } from 'mobx'

class CounterContainer {
  @observable
  count = 0

  @action
  increment = () => {
    this.count = this.count + 1
  }
}

Define a react stateless component

function CounterView(props) {
  const { container } = props
  return (
    <div onClick={container.increment}>{container.count}</div>
  )
}

Bind mobx store and react view

import mobxReactBind from 'mobx-react-bind'

const CounterComponent = mobxReactBind({
  container: CounterContainer,
})(CounterView)


// You can then just use it as a usual react component
<CounterComponent />

React lifecycle methods

In the mobx store, you can add a mount method that will be called when the component is mounted.

It also accepts a function as a return value that will be called when the component is unmounted.

class CounterContainer {
  
  mount = () => {
    console.log('mount')
    return () => {
      console.log('unmount')
    }
  }
}
0.2.4-alpha.2

4 years ago

0.2.3-rc

4 years ago

0.2.4-alpha1

4 years ago

0.2.4

4 years ago

0.2.1-rc

4 years ago

0.2.1

4 years ago

0.2.0-rc

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago