score:1

Accepted answer
class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      cusipData: []
    };

    this.socket = io("adresss");

    this.socket.on("ringBuffer", function(data) {
      addMessage(JSON.parse(data));
    });

    const addMessage = data => {
      this.setState((prevState, props) => {
        const newCusipData = prevState.cusipData;
        newCusipData[data.cusip] = data;

        return {
          cusipData: newCusipData
        };
      });

      console.log("this.state");
      console.log(this.state);
    };
  }
}

score:0

class App extends Component {
  constructor(props)
  {
    super(props)
    
    this.state = {
      cusipData: [],
    };

    this.socket = io("adresss")

    this.socket.on('ringBuffer', function(data){
        addMessage(JSON.parse(data));
    });

    const addMessage = data => {
      // adds the new data.cusip to the existing state array
      this.setState({
        cusipData: [...this.state.cusipData, data.cusip]
      });
    };
  }

Related Query

More Query from same tag