score:3
Accepted answer
array sort mutates
if you mutate there is no change detected since the reference is the same. but sort also returns itself so you can add this code. the spread operator makes a copy and there for changes the reference meaning that your changes will be detected.
const sortedresults = results.sort((a: any, b: any) => {
return (a[title] > b[title]) ? 1 : ((b[title] > a[title]) ? -1 : 0)
})
return {
...state,
swapi_data: {
...state.swapi_data,
results: [...sortedresults],
},
}
Source: stackoverflow.com
Related Query
- Why reducer with array.sort doesn't trigger re-render?
- Sort an array of objects in React and render them
- Why doesn't this select list render with the correct item selected based on the defaultValue
- ReactJS with Material-UI: How to sort an array of Material-UI's <TableRow> alphabetically?
- How to configure Why Did You Render with NextJS 12
- sort an array with react hooks
- Why do componentWillUpdate() and componentWillMount() not trigger render function when setState used in these methods?
- why filtering state array with react hooks doesn't work but filtering original array does
- Why does calling useState's setter with the same value subsequently trigger a component update even if the old state equals the new state?
- Why is my React component render called twice, once without data and then later with data, but too late exception?
- ReactJS: how to trigger reducer with action in Jest
- Why does react render an array of strings into an HTML element?
- Why does map() return an array with undefined values?
- Loop through array and render multiple instances of a child component with that data with React?
- Why cannot render HTML with ReactJS
- Why not to use splice with spread operator to remove item from an array in react?
- Why does my reducer isnt updating my store state with ImmutableJS?
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- Why React still render the child component with Error Boundary
- Why does the Sign In With Google button disappear after I render it the second time?
- How i can do bubble sort to array with object?
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Why is my reducer returning an empty array in react/redux?
- Proper way to render integer array as a string with commas in JSX
- Why doesn't a useEffect hook trigger with an object in the dependency array?
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Why does this create an infinite render loop with React hooks?
- ReactJs: How to handle empty array with map function inside render method
- Sort Array with two object inside
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
More Query from same tag
- How to access child's state in React? (React Hooks)
- Not rendering while state is changed
- Add active class on Link in react
- React Bootstrap Best Practices
- Error 403 forbidden when using React Router with Apache
- Pass props to styled-components
- How do I add a button into a stateless component?
- Keep 10 item in dimensional array in Redux
- useEffect does not trigger after initial render
- Set loading image for each item between multiple items
- Slider is not changing value
- Javascript ES6 Difference between ()=>() and ()=>{}
- using redux and redux-persist in react app: after submitting the form, store create a new and empty object
- Why is child component not rerendering in React?
- react redux two components
- Select props in Textfield material-ui
- Pass props to a child component on page load - react with next.js
- React is not hitting django apis on kubernetes cluster
- How do you debug a shallow rendered enzyme test?
- How to set the placeholder text of the MUI DatePicker?
- How to delete item from an array by onPress event in react
- How to load data in Meteor App using React?
- I'm rendering a react class Some elements seem to have a problem with closing tags but i made sure there's no unclosed tags in between what can i do?
- How to get the selected value from a radio button group in React where there are multiple groups?
- React dropdown not being populated with values from Supabase response
- Radio icon disappears on selection - using Material UI
- How to use image to replace Navbar.brand?
- How to work around updating state multiple times in a for loop? ReactJS
- Download a string as .txt file in React
- How do I test a polling react hook using Jest with mock timers?