1

i am creating a weather app in react which shows the weather forcast in a graph using the api but i am getting an error plotly is undefined although i have pasted cdn of plotly in my index.html

index.html

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />


    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

   <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>


  </body>
</html>

plot.js

import React, { Component } from 'react'

export class Plot extends Component {

    drawPlot = () => {
        Plotly.newPlot('plot',[{
            x:this.props.xData,
            y:this.props.yData,
            type: this.props.type

        }],

3 Answers 3

1

Because it is undefined in your global scope, try to console Plotly object outside of the component. If you're using Webpack for bundling try to install Plotly as an NPM package. Then import it to your project with ES6 import

Sign up to request clarification or add additional context in comments.

2 Comments

i installed plotly using npm install plotly command in my folder but it is still giving me the same error
You can also try to use react-plotly plot.ly/javascript/react as well
1

It is undefined in the global scope, install plotly by npm.

npm install plotly.js

And import it in your as

import Plotly from 'plotly.js';

GitHub Reference

Comments

0

Install "plotly.js-dist" package from npm npm install plotly.js-dist and than import Plotly from "plotly.js-dist"; in Plot.js component

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.