score:2
Accepted answer
like @clarity said, push
returns index of pushed item, also this operator mutates object. best practice is to create another array (eg. by spreading previous value, like suggested), and add a new entry there, like so:
setstate({menuhistory: [...state.menuhistory, location]});
score:1
you have defined {menuhistory: ['first']} which is array . try using concat instead of push.
setstate({menuhistory: state.menuhistory.concat(location)});
score:2
push
return an index of element, use array spread to add the item:
setstate(state => ({menuhistory: [...state.menuhistory, location]}));
Source: stackoverflow.com
Related Query
- How to push nested object interface array in React Typescript using useState
- How to push new value in array useState in react
- Javascript React: Push to an array in useState
- React - How to push array in object using useState
- React - Push array using usestate hook
- Create unique array while push Object to array in react useState hook
- push an object to an array not working on Javascript react
- Push Object to array in react useState hook
- React hooks useState Array
- React efficiently update object in array with useState hook
- React useState empty array type
- Convert object to array in Javascript / React
- Why React useState return const array
- Passing array of React components to useState returns empty array
- Concat vs push adding new array in react best practice
- React useState Hook setter has no result with array variable
- Remove first element of an array in React useState hooks?
- updating array useState react hooks (Next.js)
- React Hooks trying to useState in array ref
- Filtering an Object in Array with javascript in react native
- useState react hook does not set the array state
- How to change/add value of a certain row of an array using useState Hook in React
- React / Javascript for loop - Start from idx 0 when exceeding array length
- How to push elements to react hooks state array
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- React / Javascript map function - return array
- Correct way to add items to an array stored in React state with useState hook?
- Comparing two arrays and filtering one array upon this in javascript and react
- Javascript / React - getting first item in array that matches specific conditions
- How to push an object into an array with React Hooks
More Query from same tag
- How can I pass data between sibling components in React?
- Security of firebase realtime database rules
- django - React - Axios: Data not being saved in database
- React Isomorphic Fetch not actually posting
- JSX get Component by string
- How to bind one React Functional Component to another? (DevExtreme Chart to PivotGrid)
- cannot use .js file in a react tsx project
- How do I change Component Attribute on onClick in React?
- How to Traverse Files in React using fetch
- Export const in react
- How can I get more than one value from the Redux store in a single statement?
- ASP.NET Core DateTime Rest API with Javascript converts to UTC instead of java
- Track events in Reactjs pages and components globally
- How to set value of " this.props.history.location.pathname " manually without reloading page?
- Docker app won`t work but application works if not in docker
- React medium editor - need to click twice to be able to write if clicking on placeholder
- How to specify column width for first column using react-table
- Problem with metronic react theme to typescript
- Where to put javascript code with styled components?
- React - TypeError: Cannot read property 'map' of undefined
- Add Underline When Hover in React and CSS
- Set object in useEffect is not working in React
- TypeError: Cannot read property 'name' of undefined in ReactJS
- Adding async before functional component causes it to return an object?
- Passing state as props from Parent to Child in React-Router?
- Folder structure for React and Django REST
- React, How to output raw `null` and `undefined` and empty string `''`?
- Keycloak & React Router routing for Public/Login & Authenticated & Role based routes
- How to load the child component under the parent component
- How does my useContext component know to get the value in App.js?