0.6.3 • Published 9 years ago

tangram-react v0.6.3

Weekly downloads
1
License
MIT
Repository
-
Last release
9 years ago

Tangram

Simple, React state management

Guide

import { Model, action, store, observer, state } from 'tangram-react'
import React, { Component, PropTypes } from 'react'

class UserModel extends Model {
  @state name = 'init name'
  @state age = 23
  @state info = null
  constructor() {
    super(...arguments)
    this.fetchUserInfoFromServer()
  }
  @action addAge() {
    this.age ++
  }
  @action async fetchUserInfoFromServer() {
    return fetch('/xxxx.json').then((data) => {
        this.info = data.json()
    })
  }
}

@observer('user')
class User extends Component {
  static propTypes = {
    user: PropTypes.instanceOf(UserModel).isRequired,
  }
  render() {
    const { user } = this.props
    const { loading } = user.getActionState('fetchUserInfoFromServer')
    return <div>
        name: {user.name}
        info: {loading ? 'loading...' : user.info.address}
        age: {user.age}
    </div>
  }
}

@store({ user: UserModel })
export default class Main extends Component {
  static propTypes = {
    user: PropTypes.instanceOf(UserModel).isRequired,
  }
  render() {
    const { user } = this.props
    return (
      <div className="container">
        <User />
        <button onClick={() => { user.addAge() }}>addAge</button>
      </div>
    )
  }
}
0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.6.0-beta0

9 years ago

0.5.0

9 years ago

0.4.0-beta0

9 years ago

0.4.0

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago