score:3
Solution:
Change this line
state.photos = tableDoc;
to
setState({...state, photos: tableDoc});
Refactor code:
const newPhotos = state.photos.filter(value => value.fieldId !== fieldId);
setState(prevState => ({...prevState, photos: ...newPhotos }));
Note:
- Don't mutate data in
React Js
: The same ashandleRemove
method, you shouldn't mutate data directly, please do something like this inhandlePush
const newPhoto = {fieldId: Math.random(),file: file};
setState(prevState => ({...prevState, photos: [...prevState.photos, newPhoto]}));
- Keep in mind that, from
reactjs.org
docs say that:
Unlike the
setState() method found in class components
,useState
does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax:
setState(prevState => {
// Object.assign would also work
return {...prevState, ...updatedValues};
});
score:0
Thanks Everyone for trying to solution for me . Everyone is right. but I can modification like that:
//Old
const handlePush=()=>{
state.photos.push({
fieldId: Math.random(),
file: file , /// from upload file
});
}
}
//New
handleChange =()=>{
setState({
...state,
photos: [...state.photos, {
fieldId: Math.random(),
file: file , /// from upload file
}],
});
}
then Remove function:
//old
const handleRemove=(fieldId )=>{
const tableDoc = [...state.photos];
tableDoc.splice(
tableDoc.findIndex(value => value.fieldId === fieldId),
1,
);
state.photos = tableDoc;
}
//New
const handlePhotoRemove = fieldId => {
const tabPhotos = [...state.photos];
tabPhotos.splice(
tabPhotos.findIndex(value => value.fieldId === fieldId ),
1,
);
setState({
...state,
photos: tabPhotos,
});
};
It is successfully done!!
thanks. Plz tell me if have any easy solutions
score:1
In addition to the above solution, You can try all in a single line (no need to spread
, findIndex
, and splice
)
setState({...state, photos: state.photos.filter(value => value.fieldId !== fieldId)});
Source: stackoverflow.com
Related Query
- How to remove a single object from Array Properties in React Js?
- How to remove an element from an array that is inside an object in React JS (ES6)
- How to remove a object from an array in react hook
- React - How to access single value from array of array json object
- How can we easily remove an element from an multi dimensional array object in react js
- How can I remove an attribute from a React component's state object
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- How to delete object from array using object property - React
- Can't access nested object properties in a single object from JSON call in REACT
- How to sort array of object by properties from left to right having numbers first when property is string?
- Remove element of an object from array of objects - react state
- how to pass the current item to the function so that i can remove it from array in react component?
- How to remove elements from array using redux, immer, react
- How to select random object from array with React Hooks?
- Remove object by id from array in React hooks state
- How to remove selected index of an array from the DOM and reflect the change in React state?
- How to find most recent date from an array of object then add new field and return array in react
- How to remove item from a generic Array in React Typescript?
- How Can i Add Event Handlers In Reusable React Component Created from Object Array
- How to access a specific properties from a complex JSON array object in React.js
- Material React Select Multiple: How i can remove from my array when i click in a selected element?
- How can i update a name field from single element in array of object from a state in reducer in React-Redux?
- How to remove item from an array through index in React
- How to remove Object from Array based on ID in react?
- How To Send Array of Object using FormData() From React Js To Nodejs, MongoDB?
- How to render all object properties from array of objects
- How to return an object property from an array of objects in react
- How to remove object from Array in ReactJS after click event
- In React How can I fetch data and render a single object in a states array without mapping the whole thing?
- How to remove specific element from a array or object using ES6 spread operators and rest operators
More Query from same tag
- How can I get the value in the parent from a child components(From Inputs)
- How to get Material-UI Date Picker value
- Getting the value from a Material UI Slider component
- Material UI Grid set horizontal line instead of responsiveness
- Material UI Button - How can I use "::first-letter" pseudo element selector with MuiButton class?
- What's the '@' (at symbol) in the Redux @connect decorator?
- How to use if in input?
- how to include sass file in reactjs
- How to add anchor tag in the string passing to checkbox in react
- How to use jest mockImplemention for default export module?
- Onclick event in For loop in react
- Import TypeScript modules from local module into React app
- How to pass input value from the search bar to another component?
- Transform JSX to JS using babel
- How to use React js in blade template
- Ionic/React - How to get value from input item when it is auto-filled by the browser?
- react router / i wanna go to a new page instead of adding the component under the mother component
- React : TypeError: Cannot read property 'video' of undefined
- React onClick function for list items not receiving value when child clicked
- How add an object into the array list that is value of another object in Redux toolkit
- Should I use createRef or useRef and why?
- react-autosuggest with useRef
- Why can't I access default props in react constructor, but I can in render?
- array.map cannot export object?
- react-redux 4.0: testing mapStateToProps and mapDispatchToProps?
- React. preventDefault() for onCopy event does not work
- I can't build my react with boostrap problem about css-loader
- Typescript error in React Redux connect function
- NextJS Typescript - Setup tslint configuration to force error when have unused import/declarations
- Mocking out the theme in styled components