1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【react-obs】ReactでObserverが使えるようにしてみた

1
Last updated at Posted at 2015-11-10

Reactで小さなものをサクッと作りたいのに、コンポーネント間のやり取りがめんどくさそうだったのでRios.jsのように簡単なイベント操作でデータをやり取りできるようにしてみました。

Github - react-obs

せっかくなのでbowerにも登録してみました。

bower install react-obs

使い方はREADMEの通りなのですが、ES6でReactを使う際に継承先をちょっと変えてあげるだけでObserveのためのメソッドが使用できます。

class Foo extends ReactObs {

  constructor (props) {
    super(props);
    this.on('hello', this.A, this); // イベント登録
  }

  A (_this, message) {
    console.log(message);
  }

  B (e) {
    e.preventDefault();
    this.trigger('hello', e.target.text.value); // イベント呼び出し
  }

  render () {
    return (<form onSubmit={this.B.bind(this)}>
      <textarea name="text"></textarea>
      <button>submit</button>
    </form>);
  }
}

実際にこれでTODOアプリのデモも作ってみました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?