Reputation: 19
I need update state after sort by salary
state = {
workers: [
{name: 'Bob', surname: 'Meljanski', salary: 5140},
{name: 'Michel', surname: 'Hensson', salary: 5420},
{name: 'Jane', surname: 'Mitchel', salary: 2054}
}
startSort = () => {
const arr = this.state.workers.slice();
arr[2].name = 'Mike';
var kk = [...arr].sort((a,b) => (a.salary>b.salary)*2-1);
this.setState(kk);
console.log(kk);
}
by result. state renders without sorting. How can I do correctly? sorry for my english
Upvotes: 1
Views: 232
Reputation: 31024
setState
accepts an object (the new state object) or a function (an updater which will run when react actually updates the state, mostly used when the next state is depended on the previous state, like toggles etc), you passed to it an array.
You can try this:
this.setState({workers: kk});
Upvotes: 7