score:1
setstate
accepts objects, plain and simple. if you call this.setstate(['a','b'])
it will convert your array to an array-like object. why? because react allows that elsewhere in your code you should be able to call this.setstate({data: 'something'})
without having that fail. for reference this.state
would now look like this:
{
0: 'a',
1: 'b',
data: 'something'
}
why did you get a warning? because react performed this conversion, from array to array-like object, behind the scenes, and wants to let you know that things have changed. for example, after setting state to an array, you won't be able to call this.state.map ...
or any other array methods.
if you need to store an array in state, set it to an object property: this.setstate({arraydata: ['a', 'b']})
. in general it's a good practice to wrap your data in object properties, because you will certainly be lifting state up as you develop your application.
Source: stackoverflow.com
Related Query
- What is the issue with state being an array in react?
- React State update a nested array with objects based on the id when iterated
- React Hooks - What is the recommended way to initialize state variables with useState() from props
- React Hooks Form Handling: Update the state of an object with multiple string items and one array item
- Update the state in React app replacing the old array with new sorted array of objects
- What is the best practice with React for importing a large static array to use in a select field?
- Issue accessing state array from inside animationFrame with react
- What is the correct way to add to an array with splice in react
- Guess the number with React - what is the proper way of using state in that case
- React array being returned with no data, but there is data within the array with inspect element
- React checkbox group is not being updated with the state
- What is the best way to define "KEY" in React when creating a list with map function from an array which containing no unique ID or fields?
- Array of Objects with one of the fields being a React component?
- How do I pull data from object in array as state is being updated in React with hooks?
- React - updating an object array in the state with setState
- What is the right way to assign element of an array to state using React hooks?
- how can I change the value of an object in an array being mapped from state react
- What is the best way to deal with a fetch error in react redux?
- React with Redux? What about the 'context' issue?
- What is the best way to add a value to an array in state
- What are the differences when re-rendering after state was set with Hooks compared to the class-based approach?
- What is the difference between using constructor vs state = {} to declare state in react component?
- Typescript and React setting initial state with empty typed array
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- Updating the array object in React state using immutability helper
- Huge React State Array with Hundreds of Inputs, slow state changes onChange
- How do I use local state along with redux store state in the same react component?
- state inside useEffect refers the initial state always with React Hooks
- What is the proper way to use React Memo with Flow?
- React w/ TypeScript: why is the type for the state lost when useState with a callback as the updater
More Query from same tag
- Next JS build failing due to ''Export encountered errors on following paths"
- Pass props to nested object - Reactjs
- i'm getting an error of "Firebase Storage: Invalid argument count in `put`: Expected between 1 and 2 arguments, received 0
- Delete Request in react not deleting
- How to match string with multiple variables?
- ReactJS: TypeError: Cannot read property 'map' of undefined
- How can I set a custom height for a React Bootstrap Typeahead component?
- How to pass params to dynamic component in React?
- import jsx file in tsx compilation error
- I get the error 'Request failed with status code 400' when I use axios
- (React.js) how to get the current value of localStorage?
- Elements are in wrong position on a 're-render' caused by unfiltering a filtered array
- How to upload attachment to server ,Net Core+React(Redux)
- How to use ref in (conditional render)
- How to Access to first element within an object from each group in js with generated id?
- How to create a twitter like 3-column responsive UI?
- If using forceUpdate() is discouraged, how should components react to change events in the model?
- converting onCancel to functional component with react Hooks from class component
- Display image dynamically on hover on title with Nextjs and GSAP
- Calling functions in render method
- Next.js | Is there any way to render an .xml file?
- How bad is it to change the DOM in react?
- Apollo Client version 3 cache update delay
- How to use map/for-each for an object in react?
- Why doesn't this.props.children.map work?
- How to use Ellipsis in react-bootstrap pagination
- Dynamically import language json file for react / react-intl
- how to remove the white background in <p> element
- How to type this component in Flowtype
- How to rewrite redundant method declaration and create a shorter one?