質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

2回答

1046閲覧

Reactでapiのエラーが起きる

Shmupeiii

総合スコア105

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2022/04/26 13:01

前提

この記事を参考にして、
https://qiita.com/tatane616/items/27a53c26aad9c44819e2
ファイルを作っています。
しかし、このようなエラーが出たのですが、
フォルダを作って、apiキーを書く感じでしょうか。
よろしくお願いします。

イメージ説明

発生している問題・エラーメッセージ

JavaScript

1import React, { Component } from "react"; 2import Highcharts from "highcharts"; 3import HighchartsReact from "highcharts-react-official"; 4import apiKey from "./apiKey"; 5 6class App extends Component { 7 constructor() { 8 super(); 9 this.state = { 10 selected: Array(47).fill(false), 11 prefectures: {}, 12 series: [], 13 }; 14 this._changeSelection = this._changeSelection.bind(this); 15 } 16 17 componentDidMount() { 18 // 47都道府県の一覧を取得 19 // API Doc: https://opendata.resas-portal.go.jp/docs/api/v1/prefectures.html 20 fetch("https://opendata.resas-portal.go.jp/api/v1/prefectures", { 21 headers: { "X-API-KEY": apiKey }, 22 }) 23 .then((response) => response.json()) 24 .then((res) => { 25 this.setState({ prefectures: res.result }); 26 }); 27 } 28 29 _changeSelection(index) { 30 const selected_copy = this.state.selected.slice(); 31 // selectedの真偽値を反転 32 selected_copy[index] = !selected_copy[index]; 33 34 if (!this.state.selected[index]) { 35 // チェックされていなかった場合はデータを取得 36 // API Doc: https://opendata.resas-portal.go.jp/docs/api/v1/population/sum/perYear.html 37 fetch( 38 `https://opendata.resas-portal.go.jp/api/v1/population/sum/perYear?cityCode=-&prefCode=${ 39 index + 1 40 }`, 41 { 42 headers: { "X-API-KEY": apiKey }, 43 } 44 ) 45 .then((response) => response.json()) 46 .then((res) => { 47 let tmp = []; 48 Object.keys(res.result.line.data).forEach((i) => { 49 tmp.push(res.result.line.data[i].value); 50 }); 51 const res_series = { 52 name: this.state.prefectures[index].prefName, 53 data: tmp, 54 }; 55 this.setState({ 56 selected: selected_copy, 57 series: [...this.state.series, res_series], 58 }); 59 }); 60 } else { 61 const series_copy = this.state.series.slice(); 62 // チェック済みの場合はseriesから削除 63 for (let i = 0; i < series_copy.length; i++) { 64 if (series_copy[i].name == this.state.prefectures[index].prefName) { 65 series_copy.splice(i, 1); 66 } 67 } 68 this.setState({ 69 selected: selected_copy, 70 series: series_copy, 71 }); 72 } 73 } 74 75 renderItem(props) { 76 return ( 77 <div 78 key={props.prefCode} 79 style={{ margin: "5px", display: "inline-block" }} 80 > 81 <input 82 type="checkbox" 83 checked={this.state.selected[props.prefCode - 1]} 84 onChange={() => this._changeSelection(props.prefCode - 1)} 85 /> 86 {props.prefName} 87 </div> 88 ); 89 } 90 91 render() { 92 const obj = this.state.prefectures; 93 const options = { 94 title: { 95 text: "人口増減率", 96 }, 97 plotOptions: { 98 series: { 99 label: { 100 connectorAllowed: false, 101 }, 102 pointInterval: 5, 103 pointStart: 1965, 104 }, 105 }, 106 series: this.state.series, 107 }; 108 return ( 109 <div> 110 <h1>Highcharts React + RESAS API Demo</h1> 111 <p> 112 <a href="https://github.com/highcharts/highcharts-react"> 113 Highcharts React 114 </a> 115 </p> 116 <p> 117 <a href="https://opendata.resas-portal.go.jp/">RESAS API</a> 118 </p> 119 {Object.keys(obj).map((i) => this.renderItem(obj[i]))} 120 <HighchartsReact highcharts={Highcharts} options={options} /> 121 </div> 122 ); 123 } 124} 125 126export default App; 127

該当のソースコード

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

javascript

1{ 2 "name": "react-spa-population", 3 "version": "0.1.0", 4 "private": true, 5 "dependencies": { 6 "@testing-library/jest-dom": "^5.16.4", 7 "@testing-library/react": "^13.1.1", 8 "@testing-library/user-event": "^13.5.0", 9 "highcharts": "^10.0.0", 10 "highcharts-react-official": "^3.1.0", 11 "react": "^18.0.0", 12 "react-dom": "^18.0.0", 13 "react-scripts": "5.0.1", 14 "recharts": "^2.1.9", 15 "web-vitals": "^2.1.4" 16 }, 17 "scripts": { 18 "start": "react-scripts start", 19 "build": "react-scripts build", 20 "test": "react-scripts test", 21 "eject": "react-scripts eject" 22 }, 23 "eslintConfig": { 24 "extends": [ 25 "react-app", 26 "react-app/jest" 27 ] 28 }, 29 "browserslist": { 30 "production": [ 31 ">0.2%", 32 "not dead", 33 "not op_mini all" 34 ], 35 "development": [ 36 "last 1 chrome version", 37 "last 1 firefox version", 38 "last 1 safari version" 39 ] 40 } 41}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

回答ありがとうございます。解決できました。

投稿2022/04/26 14:43

Shmupeiii

総合スコア105

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

App.js と同じディレクトリに apiKey.js を作成し、

js

1const apiKey = "..."; 2 3export default apiKey;

と記述すれば良いと思います。

投稿2022/04/26 13:31

mather

総合スコア6753

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問