Sorting Data In Reactjs
I have a json file containing an array with some objects called json.bc shown by fetch() request. I want to sort all the objects based on total field but the data will be sort in e
Solution 1:
The whole loaded data is saved in this.state.data
(in componentDidMount
).
setState
callback calls then this.reorganiseLibrary()
- this is the place where pagination occurs, result is stored in this.state.library
and used for rendering.
To change sorting (whole set) you must sort data before pagination, in this.reorganiseLibrary()
.
Steps needed:
- save sorting order in the state (like
perPage
); - provide event handler for order change - remember to use
this.reorganiseLibrary()
insetState
callback (like inhandlePerPage
); - use ordering value for sorting in
this.reorganiseLibrary()
(beforelibrary = _.chunk(library, perPage);
) - remove
sort
parts fromrenderLibrary()
Thats all.
Post a Comment for "Sorting Data In Reactjs"