0

I made API from where I am returning values and now am trying to take fetch those values and display in pie chart using reactjs but am not able to fetch.


class Graph extends Component {
    ComponentWillMount(){
        fetch('http://localhost:3010/analysis')
        .then(response{response.json()})
        .then(data => console.log(response))
    }
    render() {
        return (
            <ReactFC
        {...pieConfigs}/>
            );
    }
}

export default Graph;

This is my code. Can anyone please tell me what are the changes I need to make to be able to display pie chart in react app?

2
  • If you want how to configure chart - npmjs.com/package/react-fusioncharts Commented Jul 15, 2019 at 7:14
  • 1
    Also make you API call in componentDidMount and instead of console.log set the data to state which can be used for chart configuration. Commented Jul 15, 2019 at 7:15

2 Answers 2

1

the problem is you fetch API right after component unmount, try

ComponentDidMount(){
    fetch('http://localhost:3010/analysis')
    .then(response{response.json()})
    .then(data => console.log(response))
}
Sign up to request clarification or add additional context in comments.

Comments

0

Example:

class Graph extends Component {
    state = { pieConfigs }
    componentDidMount(){
        fetch('http://localhost:3010/analysis')
        .then(response => response.json())
        .then(data => this.setState({ data }))
    }
    render() {
        return (
            <ReactFC {...this.state.pieConfigs}/>
        );
    }
}

Explanation:
1. Fetching data from componentDidMount, not componentWillMount. You can see this article as reference.
2. Use state/props for data related to ui. You can use state and setState to update the data and pass it as props to ReactFC. Updating state or props will trigger re-render, so that the ui will be up-to-date.

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.