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

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

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

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

Q&A

2回答

2480閲覧

Rechartsを使用したグラフが表示されない

teriyakiman

総合スコア4

React.js

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

0グッド

0クリップ

投稿2019/09/02 10:40

前提・実現したいこと

Reactで非常に簡易な計算アプリを作っています。
stateに配列を入れ、Recharts.jsを使用してグラフを描画したいです。
Rechartsの呼び出し結果、画面は何も表示されず、Chromeのコンソールを確認すると以下のようなメッセージが表示されております。
何が原因か、不明ですので、お力添えいただきたいです。

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

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-async-component-lifecycle-hooks for details. * Move data fetching code or side effects to componentDidUpdate. * If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state * Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder. Please update the following components: LineChart printWarning @ react-dom.development.js:12029 lowPriorityWarning @ react-dom.development.js:12050 push../node_modules/react-dom/cjs/react-dom.development.js.ReactStrictModeWarnings.flushPendingUnsafeLifecycleWarnings @ react-dom.development.js:12217 flushRenderPhaseStrictModeWarningsInDEV @ react-dom.development.js:24481 commitRootImpl @ react-dom.development.js:23738 unstable_runWithPriority @ scheduler.development.js:674 runWithPriority$2 @ react-dom.development.js:11834 commitRoot @ react-dom.development.js:23723 runRootCallback @ react-dom.development.js:22809 (anonymous) @ react-dom.development.js:11886 unstable_runWithPriority @ scheduler.development.js:674 runWithPriority$2 @ react-dom.development.js:11834 flushSyncCallbackQueueImpl @ react-dom.development.js:11881 flushSyncCallbackQueue @ react-dom.development.js:11869 discreteUpdates$1 @ react-dom.development.js:22941 discreteUpdates @ react-dom.development.js:2440 dispatchDiscreteEvent @ react-dom.development.js:6254

該当のソースコード

import React from 'react'; import './App.css'; import { LineChart, Line, CartesianGrid, XAxis, YAxis, Tooltip, ResponsiveContainer } from 'recharts'; class App extends React.Component{ constructor(props){ super(props); this.state={ rechartsData:[], }; } handleSubmit() { for (let count = 1; count <= 10; count++) { this.state.rechartsData.push({ data: count, hoge: count*count }); } } /* メイン処理 */ render(){ return( <div className="row no-gutters"> <div className="col-xl-8"> <div className="container-fluid"> <div className="col mb-4 shadow mt-2 pt-2 pb-4 rounded"> ここにグラフが出る!! <ResponsiveContainer width="95%"> <LineChart data={this.state.rechartsData} // 表示するデータ margin={{top: 5, right: 50, left: 50, bottom: 25}}> <XAxis // X軸 dataKey="data" /> <YAxis /> <CartesianGrid /> <Tooltip /> <Line name="乗算" dataKey="hoge" stroke="salmon" // 線の色 unit="万円" //単位 /> </LineChart> </ResponsiveContainer> </div> </div> </div> </div> ); } } export default App;

試したこと

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

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

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

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

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

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

guest

回答2

0

dataが問題だと思われます。
Reactにおけるstateは、setStateをしないと保存されませんので、handleSubmitでは、stateの値が更新されていません。(そもそもhandleSubmit呼ばれていない)
constructorのなかを、

js

1constructor(props){ 2 super(props); 3 this.state={ 4 rechartsData:[ 5 {data: '1', hoge:1}, 6 {data: '2', hoge:4}, 7 ], 8 }; 9 }

にして見てください。描画されると思います。
heightも指定してあげてください

投稿2019/11/09 07:41

YukiSaegusa

総合スコア35

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

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

0

ResponsiveContainerのheightを指定していないためではないでしょうか?

投稿2019/11/07 05:31

chrome37

総合スコア6

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

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

teriyakiman

2019/11/09 00:24

ご回答いただき、ありがとうございます。heightを設定しても、グラフは描写されず、、、以下のようなエラーがコンソール表示されます。 react-dom.development.js:12029 Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-async-component-lifecycle-hooks for details. * Move data fetching code or side effects to componentDidUpdate. * If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state * Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder. Please update the following components: LineChart
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問