S
S
semki0962020-02-11 10:48:23
React
semki096, 2020-02-11 10:48:23

How to work with dynamic data?

I'm trying to figure out the d3js + react library. Namely, the question of working with dynamic data. Here is the function that receives the data

......
export function getData() {
  const promiseMSFT = fetch("https://......tsv")
    .then(response => response.text())
    .then(data => tsvParse(data, parseData(parseDate)))
  return promiseMSFT;
}
......

And here is the component that renders the graph using this data
import { getData } from "./getdata"
......
class ChartComponent extends React.Component {
  /*componentDidMount функция вызывается после рендеринга компонента*/
  componentDidMount() {
    getData().then(data => {
      /* setState - для асинхронного обновления данных? */
      this.setState({ data })
    })
  }
  render() {
        ...

As far as I understand, the graph is based on the data obtained in the form of an object. But what if I need to give live data, for example, through sockets? I would be grateful for hints and ideas. I don't even know how to approach this problem. If, for example, I give data every minute, how can I not lose the old data and add new ones? I don't even know where to start.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-02-11
@hzzzzl

How can I not lose the old data and add new ones

this.setState({ data: [ ...this.state.data, ...newData ] })
// новый массив [ старые данные + новые данные ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question