question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow state to be an object or an array

See original GitHub issue

I would like to propose that React allows state to be an object or an array (or maybe even a simple value such as number or a string or a boolean). The reason I am proposing this is that I thought it was already possible (I think of setState sort of like patching a JSON and JSON allows top level arrays and simple values), but I found our the hard way it was not this morning.

I can set the initial state just fine like this:

type MyComponentState = { value: string; }[];
class MyComponent extends Component<{}, MyComponentState> {
  public readonly state: MyComponentState = [];
}

However when I issue a state change such as this:

this.setState(state => [ ...state, { value: 'new' } ]);

The state will then be an object with key 0, not an array! This is not caught by the React TypeScript typings, nor by React itself (telling me I cannot do this in the constructor nor in setState as a runtime check - not even in the development bundle, which otherwise checks many things, event things like mounting to body etc.). So the only way to find out is to wonder why this.state.map is undefined in the render that follows the setState call merge.

Thanks for considering this!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
nmaincommented, Mar 21, 2019

useState does everything you want; the state value can be of any type and there’s no automatic Object.assign() going on behind the scenes.

0reactions
TomasHubelbauercommented, Mar 25, 2019

I really hope the existence of Hooks (which were supposed to be just an RFC and testing the waters AFAIK just a few weeks ago [even though they immediately became the part of the documentation…]) will not prevent new development on class based components.

I know in your blog posts you said those are not going anywhere (probably ever), but I also hope they will continue being actively developed.

I am not talking about this issue specifically, IMO it’s a good QoL improvement but not that important. In general though, just because something is easy with hooks shouldn’t IMO mean that we stop to look for ways to make it nice with class components, too. Many people will not learn hooks and many people will not use them out of principle, because not everyone thinks they are a good idea. It would be a mistake for them to “taint” the rest of React if they became and argument againstimprovement to class components in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Data into an Array in a State Object | Pluralsight
A state object can store arrays that can be updated with a response from an API or users. React tracks the changes in...
Read more >
Updating Arrays in State - React Docs
Just like with objects, when you want to update an array stored in state, you need to create a new one (or make...
Read more >
How to manage React State with Arrays - Robin Wieruch
Learn how to manipulate arrays in React state by using JavaScript array methods such as concat, map and filter.
Read more >
how can I use a state as an array of objects - Stack Overflow
I thought about state (I don't know much about states, just started learning it) so I can put an array of object inside...
Read more >
How to Add to an Array in React State using Hooks
The most simple solution is to use immutable objects. If the object needs to be updated, a new object with the new value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found