React-chartjs-2: Failed prop type: Invalid prop `data` supplied to `ChartComponent`.

Created on 15 Jun 2018  路  2Comments  路  Source: reactchartjs/react-chartjs-2

Hello,
I'm getting the error in the title but I can't figure out the reason.

In my React app, I have this line of code inside the render():

<Bar data={this.state.arduData} width={100} height={50} options={{ maintainAspectRatio: false }} />

and also in my this.state.arduData I've stored an array like this one: { labels: ['a','b','c','d'], datasets: { label:Data from arduino ${this.state.selectedArduino}, data: [1,2,3,4] }

I have a state property called data, maybe I'm having problems due that? Thanks in advance

Most helpful comment

@alexgbor I was having the same issue. In my component that calls <Bar />, I was initializing the state like this:
this.state = { data: [] };
and updating the state in componentDidMount(). This initialization was throwing an error: ChartComponent requires an object type and not an array type.

I switched my state initialization to this:
this.state = { data: { datasets:[], labels:[] } }
and I'm no longer getting the error in console.

Try outputting to console what your this.state.arduData value is during render() and see if this is what I was running into. Hope that helps!

EDIT: fixed my garbage formatting

All 2 comments

@alexgbor I was having the same issue. In my component that calls <Bar />, I was initializing the state like this:
this.state = { data: [] };
and updating the state in componentDidMount(). This initialization was throwing an error: ChartComponent requires an object type and not an array type.

I switched my state initialization to this:
this.state = { data: { datasets:[], labels:[] } }
and I'm no longer getting the error in console.

Try outputting to console what your this.state.arduData value is during render() and see if this is what I was running into. Hope that helps!

EDIT: fixed my garbage formatting

That was exactly my problem. Thanks man

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LuizMoreira picture LuizMoreira  路  3Comments

cbroberg picture cbroberg  路  5Comments

flxwu picture flxwu  路  3Comments

flyingpath picture flyingpath  路  5Comments

nealoke picture nealoke  路  5Comments