I am attempting to create a reactJS state that has an empty array in it on construction. Once i receive a message, that is a JSON object, I would want to create a key in that array and store that received message as the value under that key.
for example. I have
array x =[]
JSON_obj = {"data":"mydata"}
in normal javascript I could say
x["some_key"] = JSON_obj
I am attempting accomplish this in reactJS inside the state.
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({ cusipData: this.state.cusipData[data.cusip], data });
console.log("this.state");
console.log(this.state);
};
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>