Rxjs-working-with-rxjs-and-reactjs

提供:Dev Guides
移動先:案内検索

RxJSとReactJSの操作

この章では、RxJをReactJSで使用する方法について説明します。 ここでは、Reactjsのインストールプロセスについては説明しません。ReactJSのインストールについては、次のリンクを参照してください:/reactjs/reactjs_environment_setup

以下の例で直接作業します。RxJSのAjaxを使用してデータをロードします。

index.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import { ajax } from 'rxjs/ajax';
import { map } from 'rxjs/operators';
class App extends Component {
   constructor() {
      super();
      this.state = { data: [] };
   }
   componentDidMount() {
      const response = ajax('https://jsonplaceholder.typicode.com/users').pipe(map(e => e.response));
      response.subscribe(res => {
         this.setState({ data: res });
      });
   }
   render() {
      return (
         <div>
            <h3>Using RxJS with ReactJS</h3>
            <ul>
               {this.state.data.map(el => (
                  <li>
                     {el.id}: {el.name}
                  </li>
               ))}
            </ul>
         </div>
      );
   }
}
ReactDOM.render(<App/>, document.getElementById("root"));

インデックス

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "UTF-8"/>
      <title>ReactJS Demo</title>
   <head>
   <body>
      <div id = "root"></div>
   </body>
</html>

このUrl-https://jsonplaceholder.typicode.com/usersからデータをロードするRxJSのajaxを使用しました。

コンパイルすると、表示は以下のようになります-

RxJsとReactJS