LoginSignup
0
1

More than 1 year has passed since last update.

Reactとの連携 7 完成

Posted at
app/javascript/pack/***jsx
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'

var target_dom = null;

document.addEventListener('DOMContentLoaded', () => {
   target_dom = document.querySelector('#data');
   const url = new URL(location.href);
   let f = url.searchParams.get("name");
   if (f == null){ f = ''; }
   getData(f);
});

function getData(f){
  let url = "http://localhost:3000/data/ajax";
fetch(url)
  .then(
    res => res.json(),
      (error) => {
        const el = (
          <p>ERROR!!</p>
        );
        ReactDOM.render(el, target_dom);
    }
  )
  .then(
    (result) => {
      console.log(result);
      let arr =[];
      for(let n in result.rss.channel.item){
        let data = result.rss.channel.item[n];
        arr.push(
           <tr>
             <th>{data.title}</th>
             <td class="small">{data.pubDate}</td>
           </tr>
        );
      }
      const el = (
             <table class="table mt-4">
               <thead class="thead-dark">
                <tr><th><a href={data.link}>{data.title}</a></th>
                <th>Date</th></tr>
               </thead>
                <tbody>{arr}</tbody>
             </table>
      );
      ReactDOM.render(el, target_dom);
    },
    (error) => {
      console.log(error);
      const el = (
        <p>ERROR!!</p>
      );
      ReactDOM.render(el, target_dom);
    }
  );
}

20210503-164336.png

0
1
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
0
1