score:0
you can achieve this by using spread operator. it can be added as follows:
this.setstate({
file1: [...this.state.file1,{key1:'val1',key2:'val2',key3:'val3'}]
})
score:1
you should use the setstate
function expression, so you can access the previous state:
addfile(newfile) {
this.setstate(state => ({
files: {...state.files, ...newfile}
})
}
newfile
should be an object that looks like this:
{
file2: {
...
}
}
this object gets merged into the previous state. if there already was an entry with the same key (file2
), it would get overridden.
Source: stackoverflow.com
Related Query
- How to push a new object into state without removing the previous values in React
- How to switch between object keys and clone the previous values before adding new values in react
- How to push values into state by calling single onChange function - react
- How to push a new value into the current array using the react hooks?
- How can I store previous values instead of storing the value in array of objects in react state
- How do I take an array of objects from state and push a specific value of the objects into a new array
- how to get form values into state object in react
- How to keep the updated state array of object values when update the same state again in React JS useState?
- How can I add new a new object with key and values pairs to the existing state in class based component?
- How to create an object from another class, and push it into the state array?
- How to add new object into existing state using react and redux saga
- How can I copy the state in react without reference?
- How to push into an array of object using the spread operator to a specific element
- How to use nested routes to add content to a page without removing the content of the previous route with react-router-v4?
- Is it ok to pass the entire Redux state object into a React component?
- In JSX | React How can the prop be passed to the button state so the button state can load a new page?
- How to maintain previous changed state in REACT JS as when new item is updated, previous is set to default?
- How does calling "setState" as a callback in a function, without any new state value, successfully update the state?
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to update the state of nested json object in react using hooks
- How do I combine two arrays in react to get a new one with all of the items from the previous two?
- how to set state array of object without removing old state
- How to push an object into an array with React Hooks
- How can I redirect to auth0 without clicking on the Log In button in react using the new auth0-spa.js?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How I can loop through the similar objects in state and push them into object?
- Looping through two object arrays to push matching values to a new array doesn't work in React but does in JS Fiddle
- How can I update component in React after adding a new item into the database, using react hooks?
- How can I redirect to another component in react and pass the state that I set in the previous component?
- How to add values from state into a object
More Query from same tag
- dont want return children on get list mongodb
- ReactJS: update record of an object of an array within an array
- Convert string React Component to jsx again
- How can I get option value from react select?
- npm compiling warning (Parsing error: Unexpected token, expected ";" )
- ReactJS recieving form errors from server
- What are the best practices to initialize a Next.js webapp with data from an API and adding it to the state?
- React Memory Leak Although i return a clearinterval?
- Graphql query returns null id in mongodb aggregation
- How can I pass random prop from api to another component?
- heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" | Error occurs when deployed in Heroku
- React use custom hook function
- get the response of a POST API in React
- Type '() => void' is not assignable to type 'string'
- My own external js codes work once in react-router
- Get a Ref to BlockListBlock in the block editor
- Unexpected token when mapping in React
- Not able to set state in react properly
- Displaying Data Based on Ternary Operator in React
- Using history.push() while using a component within another component
- how to change json file import path in javascript
- React: data is not fetched anymore after page is reloaded
- How to call and form a React.js function from HTML
- Can't acess object attribute outside function
- Activity needs to be set if initial lifecycle state is resumed
- framework7-cli generated app dev server doesn't update
- running a useEffect after a click even if the url trigger isn't changed
- Existing React/Node project - adding AWS authentication
- PDF file upload from react-hook-form won't open with SendGrid API
- How To Get startDate and endDate from RangePicker from ANTD.DESIGN