d3.csv returns index.html content in console.log

好久不见. 提交于 2020-01-02 11:04:24

问题


I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:

import React from 'react';
import * as d3 from 'd3';

class List extends React.Component{

componentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
    console.log(d);
});
}

render(){
        return(
        <div> </div>
        );
    }
} 
  export default List;

and List is being imported in following 'App' class

import React, { Component } from 'react';
import logo from './logo.svg';
import List from './components/list/List';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>          
        <List/> 
      </div>      
    );
  }
}
 export default App;

Following is the 'index.js' code:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.


回答1:


You could try using the import statement to load the CSV file at first. :)

import React from 'react';
import * as d3 from 'd3';
import data from "./data/data.csv";

class List extends React.Component{
  componentDidMount(){
    d3.csv(data).then(function(d, error){
      if (error) {
        console.log(error);
      } else {
        console.log(d);
      };
    });
  }

  render(){
    return(
      <div> </div>
    );
  }
} 

export default List;


来源:https://stackoverflow.com/questions/50000964/d3-csv-returns-index-html-content-in-console-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!