Skip to content
Advertisement

How to in React Component constructor set this state

I still learn much React JavaScript and now I can’t understand how to create this initial state.

In the constructor here in the code I want to add to state by running this line:

JavaScript

And direct after I want to add more state variables like this:

JavaScript

The problem now is that state does not contain the first call to CsvViewer. How can I add to state both the call to CsvViewer and the other state variables?

Code:

JavaScript

Advertisement

Answer

There’s a few options for adding properties to an object. Here’s the one i would usually do, using spread syntax. It will create a shallow copy of the object returned by parse, and add in the extra properties you define:

JavaScript

Object.assign can be used to do something similar:

JavaScript

Or you could just add the individual properties one at a time:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement