score:0
I think the problem may come from the fact that you try changing this.state
directly in your method sortHandler
before passing it to setState
.
Try cloning this.state.data
first: var clone = this.state.data.slice(0);
Then manipulate (sort
) it, and finally assign it to your new state with this.setState({ data: clone })
Also, you may avoid calling multiple times this.setState()
in your method, rather once at then end of your function.
score:0
I found several problems here.
First: don't assign state by this.state['arg']=
. It's a bad practice. Use this.setState()
instead.
Second: this.setState()
it's asynchronous function, don't forget. Use callbacks. this.setState({ data }, () => anotherFunction())
In your case you can use lodash cloneDeep()
method. It return deep copy of object, so you will not change original object. const data = _.cloneDeep(this.state.data).sort()
.
So. this.setState({ sortKey: e.target.dataset.sortcolumn }, () => putYourSwitchHere());
After that,
this.setState({sortOrder: "ascending"});
this.state.data.sort((a, b)=>{
if (a[sortKey] < b[sortKey]) {
return -1;
}
if (a[sortKey] > b[sortKey]) {
return 1;
}
return 0;
});
break;
Try to use:
const data = _.cloneDeep(this.state.data).sort((a, b)=>{
if (a[sortKey] < b[sortKey]) {
return -1;
}
if (a[sortKey] > b[sortKey]) {
return 1;
}
return 0;
});
this.setState({sortOrder: "ascending", data});
break;
You have already assign this.state.data
, this.state.data = this.props.data
will make a lot of errors in your last case.
score:0
The problem is in sortHandler
function.
You are sorting the data using javascript sort()
method. It is mutating method and doesn't create new object so React.js can't detect that the data has changed.
You should use non-mutating method.
Your code should be
const newData = this.state.data.concat().sort(...);
this.setState({data: newData});
Source: stackoverflow.com
Related Query
- React table with sorting and pagination doesn't update
- React table dynamic page size but with size limit and pagination
- Handle Pagination Sorting and Filtering Separately in Antd Table React
- Sorting React table by clicking table headers for columns with numerical and string values
- React Redux with Router Location, Pagination / Sorting and Fetch
- Update a Table with React Hooks when a Row is Added, Deleted and Modified? [Issue: Gets Before Post and Delete]
- clear state and update with newdata while navigating to next page with pagination in React JS
- TypeError: Object(...) is not a function with React Table and moment.js
- React with Material UI Table Pagination show all entries
- How to add sorting to table with react virtualized?
- How to test redux state update with react testing library and jest
- React Convert Div and Table Component Into PDF With Multiple Pages and CSS
- React Redux: Update and replace records with another records returns value of 1
- Header flickering and freezing issue with material table (implemented with react window + react window infinite loader)
- How to use the history to update the pagination bar with react router?
- Test an whole table React component cells with jest and Enzyme
- Selection and Remote Data Pagination on Material Table React
- How to run React useEffect only once and property update state with setState
- Trouble with defaultSorted and React Bootstrap table Next
- Reordering table columns with react via drag and drop
- Update just one value of JSON object and keep the old ones with React Flux
- Separate filter and sorting button for react table
- How to update state array without refresh and handling with API requests React way?
- How to test a React component that update over time with Jest and Enzyme?
- React Hooks Form Handling: Update the state of an object with multiple string items and one array item
- React sync with local storage and update dynamically
- How to read and update React Context from child with function components?
- React Hooks and Fetch: How to update a table based on a user input from an API search
- testing react function with state update on input text using jest, rtl, and ionic
- How to handle react state and pros update appropriately with reducer
More Query from same tag
- Not able to access array elements in react JS
- ESlint - Error: Must use import to load ES Module
- how to re-render after getting axios response in jest test
- How to wait for react asynchronoes setStates
- Redux-Saga Getting undefined on componentDidMount()
- post menthod not working when send data from reactjs to django restful api
- gatsby-plugin-sitemap Putting trailing slash to homepage only
- How to remove trailing / on root route with react-router
- React JS Adding Data to an Array through Functions
- How to a use bootstrap grid for an array created by a map method in react?
- Redux-Saga is not waiting for action to complete using redux-saga-firebase
- copy entire object and change one property
- How to integrate whatwg-fetch in Gatsby
- How to shallow render everything except sub react components or specify a depth
- Not able to navigate when token is null in react js
- React Module not found: Can't resolve '../utils/api'
- need to access url inside header component in react
- How to get correct MouseHover Coordinates on datamaps
- How to temporarily turn on/off React development mode when using webpack?
- How to prevent axios from posting before state is updated
- gtm.js not firing for page view
- Filtering objects based on the value of an array of objects
- Beginner React query(How to remove a element and append another element)
- searchWords.filter is not a function
- React router v4 nested route relative path
- how to add bootsrap link in specified reactJS file?
- How to stop already logged in user to go back to the sign-in page in React.js
- ProtectedRoutes function. How to call redux useSelector()
- update the defaultValue on re
- Missing "key" prop for element in iterator react/jsx-key in React