npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

observable-react

v0.0.5

Published

Use react with rxjs

Downloads

2

Readme

Observable React

在 React 项目中更方便的使用 rx.js

开始使用

npm install observable-react

在组件的 constructor 中进行初始化:

import setupObservables from 'obserbvable-react';

class Demo extends React.Component {
    constructor(props) {
        super(props);

        setupObservables(this);
    }
}

生命周期

初始化之后,生命周期被转化为以下生命周期流

this.componentWillMount$
this.componentDidMount$
this.componentWillReceiveProps$
this.shouldComponentUpdate$
this.componentWillUpdate$
this.componentDidUpdate$
this.componentWillUnmount$
this.componentDidCatch$

你可以像这样使用:

const clock = Rx.Observable.interval(1000).takeUntil(this.componentWillUnmount$);

你也可以跟以前一样使用 React 的生命周期函数,不冲突。

快速将 Observables 绑定在 state 上

this.setState$({
    foo: foo$,
    bar: bar$,
});

或者

this.setState$(Rx.Observable.of({
    foo: 'foo',
    bar: 'bar',
}));

组件事件

快速添加事件流

<Button onClick={this.ObservableEvent('buttonClick')} />

调用 this.ObservableEvent 会创建一个事件流: this.buttonClick$,在 componentDidMount 中可以使用:

this.buttonClick$.subscribe(([event, nativeEvent]) => alert('按钮被点击!'));

this.ObservableEvent 创建的事件流默认发送组件的原生事件,你也可以为其添加参数来进行自定义:

  • this.ObservableEvent('buttonClick', (event) => event.target) 转化组件原生事件
  • this.ObservableEvent('buttonClick', someData) 发送自定义数据
  • this.ObservableEvent('buttonClick', someData, otherData) 发送多个数据;发送时这些数据会被拼合成一个数组

预定义事件

可能有些组件是动态加载的,那么在 componentDidMount 中该事件流可能还未定义。可以在 constructor 中预先定义事件,以免出现报错。

this.buttonClick$ = new Rx.Subject();

Ajax 请求

从 Ajax 请求快速创建数据流:假设 fetchRemoteData 是一个网络请求方法,返回 Promise。

this.remoteData$ = this.createObservableRequest(fetchRemoteData, arg1, arg2);

或者

this.ObservableRequest('remoteData', fetchRemoteData, arg1, arg2);

这样创建的数据流不会立即进行请求,而是有观察者订阅时才会进行发送第一次请求。 数据流可以进行刷新:

this.remoteData$.request();

假如每10秒刷新一次:

Rx.Observable.interval(10000)
    .subscribe(() => this.remoteData$.request());

也可以为请求传入新的参数:

this.remoteData$.request(newArg1, newArg2);