D
D
Drunk_bear642020-02-12 13:09:14
React
Drunk_bear64, 2020-02-12 13:09:14

How to update the recharts chart?

Hello, I have a state with points, when I add a new point, the graph is not redrawn. How can this be implemented? I've only been working with react for a couple of days

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './App.css';
import {
  LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend,
} from 'recharts';

class App extends Component {
  constructor(){
    super();
    this.state = {
      data: [ 
        {
          name: 'Page A', a: 2400,
        },
        {
          name: 'Page B', a: 1398,
        },
        {
          name: 'Page C', a: 2398,
        }
      ],
    }
  }
  func (){
    const {data} = this.state
    const newObject ={name: 'Page C', a: 3398,}
    data.push(newObject)
    this.setState({data: data});
    ReactDOM.render(<App />,document.getElementById("root"))
  }
  test(datatest){
    return (
      <div>
        <LineChart
          width={1000}
          height={600}
          data={datatest}
          margin={{
            top: 5, right: 30, left: 20, bottom: 5,
          }}
        >
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="name" />
          <YAxis />
          <Tooltip />
          <Legend />
          <Line type="monotone" dataKey="a" stroke="#8884d8"/>
        </LineChart>
      </div>
    );
  }
  render(){
    const test = this.test(this.state.data)
    console.log(this.state.data)
    return (
      <div className="App">
        {test}
    <button onClick={() => this.func()}>{this.state.data.length}Нажми на меня</button>
      </div>
    );
  }
}

export default App;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question