score:2
It's not recommended to modify the state itself because it is immutable.
So instead using .pop()
on the original state of the array, first I suggest to clone that one and remove the required element from there, then the result should passed to setList()
function.
Try as the following instead:
const remove = () => {
const copy = [...list];
copy.pop();
setList(copy);
}
Think about the following:
const list = [1,3,5,6,7];
const copy = [...list];
copy.pop();
console.log(list);
console.log(copy);
I hope this helps!
score:2
You need to set a new array in this case, setList(list)
will not cause React to re-render because it's still the same array you're using.
Try setList([...list])
in your remove
function.
There's also an alternative to pop
, and doesn't mutate the original variable:
const remove = () => {
const [removed, ...newList] = list
setList(newList)
}
Source: stackoverflow.com
Related Query
- Why the following react functional component does not work properly?
- React input[type=date] component does not work properly
- React Admin: onSuccess does not work properly on <Edit> Component
- React Hook does not work properly on the first render in gatsby production mode
- Why does useEffect React Hook not work properly with dependency?
- <element>.innerText in component method code does not work properly in enzyme testing. Testing react component with Jest+Enzyme(mount())
- Why does the component is not re-rendering when i add @action decorator to an react event handler function
- Why callbacks in react functional component is not reading the updated state value
- React test passes but component does not work properly when state updated in document.addListener handler
- React functional component using the `useState` hook does not update onChange using ReactiveSearch
- React Redux, my component is not connected to Redux Store. I tested the action and reducer in another component, it does work for other components?
- Why does react display my functional component at the bottom of my page?
- Why does my state not change or work the first time I click submit in React
- socket connect event inside useEffect does not work when component is created but works after refreshing the page in React
- Why does not the background color get applied from props using state for a styled component div using react and typescript?
- Why does sort not work in React component
- Why does calling react setState method not mutate the state immediately?
- Why is `Promise.then` called twice in a React component but not the console.log?
- Why does my React Component Export not work?
- Why does React warn me against binding a component method to the object?
- Why does my Animated view not stay in the correct place in React Native?
- this.node.contains does not work if this.node is a ref to react component
- React - component in seperate script does not work
- My React component does not update in the Safari browser
- In React why does calling a function from another function in our code not work and display?
- Why does react call render function if I have not changed the reference of the state?
- What is the benefit of having $sce or Strict Contextual Escaping in angularjs and why react does not need to do it?
- Advantages of functional component in React? And why the react team suggest it's the feature of react components?
- Why does custom Button component using MUI Button not work on hover with Tooltip?
- Why does my React app append the external URL to http://localhost:/<port> and not to ONLY the URL itself?
More Query from same tag
- React: Pass component as prop without using this.props.children
- React JS Error "...rest" Syntax error: Unexpected token
- React native buld failed with the following message Failed to list versions for com.facebook.react:react-native
- How to filter fetched data with checkbox in react hook
- Authentication component for ReactJs
- how to use multiple react spring bottom sheet?
- Passing Route path to the children element
- How should I style my singular react components using an external stylesheet?
- React - JSX conditional tags
- Change rendered components depending on state
- ReactJS - pass values to a component
- this.props.dispatch is not a function. Not able to dispatch mapdispatchtoprops
- React array not updating when deleting or editing an item, from database
- ReactJS Routing - Update Router, when Router and Link are in separate components
- Object doesn't support property or method 'scrollBy' in IE11 in React
- Best way to handle useRef(null) in typescript
- Unable to use div components with bootstrap classes inside map function in react
- Looping through certain elements in React
- how to Insert multiple Image into react-boostrap modal?
- How to redirect with react-router while preserving initial query parameters?
- Large images not rendering with Webpack + React?
- React - Slide out component on unmount
- Cannot read property 'selectionEnd' of null in aoutoComplete material UI
- react className conditional setting according to all of state
- ButtonToolbar with inline words in react-bootstrap?
- How do we mock fetch in Redux Async Actions?
- I have an api call that I need to invoke before the second render after it receive new props
- How to parse data through a button from one component to another
- React component is not accepting JSX Element as prop
- Issue with this.props properties not existing even though they do