score:0
You can keep the list unchanged and use another state to update the ui:
const [list, setList] = useState(list);
const [count, setCount] = useState(list.length);
useEffect(() => {
list.push(newItem);
setList(list);
setCount(list.length);
}, [newItem, list]);
score:1
The general approach of using the setter's callback instead of having to refer to the outer state value is perfectly normal and acceptable. You can slim it down too:
useEffect(() => {
setList(prevList => [...prevList, newItem]);
}, [newItem]);
But it's a little bit strange to have a separate newItem
state that immediately gets merged into the list. Consider if you could merge them together so you only have one stateful variable in the end. Instead of doing
setNewItem(theNewItem);
do
setList(prevList => [...prevList, theNewItem]);
and then, wherever else you refer to newItem
, change it to list[list.length - 1]
- or do
const newItem = list[list.length - 1];
in the body of the component.
That approach won't work for all situations, but consider whether it'd be applicable to yours.
Source: stackoverflow.com
Related Query
- React useEffect: Prevent crashing of app when updating state that is required in the dependency array
- Prevent infinite loop when updating state via React useEffect Hook
- useEffect - Prevent infinite loop when updating state
- when using react useEffect my app is crashing
- Prevent useEffect retriggering when updating state from specific function
- React Router. How to prevent crashing whole app when have any error on of the container(page)
- React Hook useEffect has a missing dependency when updating state based on current value
- When the Next js app refreshing, useEffect not dispatching redux saga action and updating the state
- State not updating when using React state hook within setInterval
- When testing, code that causes React state updates should be wrapped into act
- React component not updating when store state has changed
- React shouldComponentUpdate() is called even when props or state for that component did not change
- Props not updating when redux state change in React Hooks
- Component not updating when I change the props that I pass to it in React
- Updating the react component state when a global variable changes
- useEffect not being called and not updating state when api is fetched
- Too much recursion when updating state in react
- Why is my react project crashing when updating from react-scripts 3.2.0 to 3.3.0?
- React - useEffect running even when there was no change in state variable
- React is not updating when Redux state is changed below the first level
- setInterval updating state in React but not recognizing when time is 0
- useState setter not updating state when called in useEffect
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- Prevent re-rendering when state is changed multiple times during useEffect
- React Hooks: Idiomatic way to ensure that useEffect runs only when the contents of an array argument to the custom hook change
- React setState() not updating state when button clicked
- React useState when updating state twice, first update gets deleted
- How to re-render react component when mapping over state that is array of objects
- When testing, code that causes React state updates should be wrapped into act(...)
- React Hooks: state not updating when called inside Socket.io handler
More Query from same tag
- Loading a local React app inside WKWebView iOS
- Fetch with React on codesandbox : cross-origin error
- error saying:- Idling, State changed from up to starting, Stopping all processes with SIGTERM, Process exited with status 143
- Pulling props up before component in React
- Cannot read property 'push' of undefined - React error
- Setup React/DRF without using CORS or CSRF tokens
- Handle click outside of React component and how to add animation to it?
- Adjusting the range of a slider based on handle positions
- How to dispatch actions from Child components three level down?
- Can't enable and focus input at the same time
- Possible to Interact with Custom Javascript Object Handler
- i want to build sidebar on my own but the function don't work
- How to remain state when page refresh in react?
- want to redirect to page after the action is dispatched in useEffect
- how do I set logging levels on Android, for react native?
- How to set React app and API on same port?
- Uncaught TypeError: can't define property "x": Object is not extensible
- Removing an item from an array of items only removes the last item
- React + Material UI + Typescript + Webpack + SSR not working
- How to deploy React app with docker and serve -s build
- Modal works after second click
- Using Array.find() to try and dynamically grab a specific object value and return it based on Object name, nothing returning from function
- React Hook useEffect has a missing dependency: 'user.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps
- How to get start and end timestamps of every month in selected year
- Test button when it is clicked - React testing library
- Maximum update depth exceeded with useLayoutEffect, useRef
- TypeError: Cannot read property 'map' of undefined on Netlify CMS/React.js
- Delete list item ReactJS
- How to change Material UI stepper background color if there is an error in a step
- React Router + iis? How to enable routing