【问题标题】:Capture click events on React Native with RxJS使用 RxJS 在 React Native 上捕获点击事件
【发布时间】:2018-03-07 10:17:08
【问题描述】:

互联网上有很多例子,比如here,展示了如何使用 RxJS 捕获网页中的点击事件。

我测试了这些示例,它们都可以正常工作,即使在 React.js 应用程序中也是如此,但它们不适用于 React Native 应用程序,因为 React Native 应用程序没有 DOM。

有没有办法通过 RxJSReact Native 应用程序中使用 Rx.Observable.fromEvent 来捕获点击事件? (不是 React.js)

【问题讨论】:

  • 您找到解决方案了吗?

标签: react-native rxjs


【解决方案1】:

一种可能的解决方案是在 OnPress 函数中使用 Subject 来执行此操作,并在 ComponentDidMount 函数中订阅它,这里有一个快速示例(也可以使用here)。希望对您有所帮助。

import { Subject } from 'rxjs';

class TestPage extends React.Component {
  constructor(props) {
    super(props);
    this.subject = new Subject();
  }

  componentDidMount(){
     this.subject.subscribe(data => console.log(data), err => console.error(err))
  }

  onPress =(item) => {
    this.subject.next(item)
  }

  render() {
    return (<View>
        <Button onPress={this.onPress} />
      </View>)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多