Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
React.js setState en la carga de la página no funciona, cómo solucionarlo

Estoy usando react-apexcharts para crear un gráfico circular. Tengo éxito en la representación de la serie = [] porque es nativa del componente apexcharts.

Pero estoy tratando de representar también las etiquetas, que es una matriz simple.

Si ejecuto el archivo console.log:

 console.log(this.props.total.map(a=>a.name))

prestar

 ["label 1","label 2","label 3"]

¿Cómo puedo actualizar la label: [], con los valores de this.props.total.map(a=>a.name) ?

El código completo está abajo

 import React from 'react' import { Card, CardHeader} from 'reactstrap' import Chart from 'react-apexcharts' class PieChart extends React.Component { constructor(props) { super(props); this.state = { series: [], options: { chart: { width: 380, type: 'pie', }, labels: [], theme: { mode: 'light', palette: 'palette2', }, legend: { show: true, position: 'bottom', horizontalAlign: 'center', floating: false, } }, }; } componentDidMount() { this.setState({ label: this.props.total.map(a=>a.name), }) } render() { return ( <> <Card color="gray" className="mb-3"> <CardHeader className="bg-gray-lighter">PieChart</CardHeader> <Card body> <div id="chart3"> <Chart options={this.state.options} series={this.props.total.map(a=>a.total_today_brl)} type="pie" height={450} /> </div> </Card> </Card> </> ); } } export default PieChart;

Intenté sin éxito:

 <Chart options={this.state.options} series={this.props.total.map(a=>a.total_today_brl)} labels={this.props.total.map(a=>a.name)} type="pie" height={450} />

Y probé también:

 options: { chart: { width: 380, type: 'pie', }, labels: this.props.total.map(a=>a.name), theme: { mode: 'light', palette: 'palette2', },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

labels están debajo de options

entonces, para actualizar la etiqueta en el estado, debe hacer

 this.setState((prevState)=> ({ ...prevState, options:{ ...prevState.options, labels: this.props.total.map(a=>a.name) } }))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!