# \[Solved\]React does not re-render after state change

**URL:** <https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [November 15, 2016, 8:49pm UTC](https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714 "2016-11-15T20:49:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hristijankiko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hristijankiko/32/25293_2.png) [@hristijankiko](https://forum.freecodecamp.org/u/hristijankiko)\
**Post date:** [November 15, 2016, 8:49pm UTC](https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714/1 "2016-11-15T20:49:10Z")

</div>

I finished the Freecodecamp Leaderboard challenge but there is a thing bothering me. In the documentation it said that using element.forceUpdate() is not recommended. Without using the forceUpdate() function my element did not re-render even though the state was changed(checked in console.log()). Does React not count reordering of array as a change big enough to re-render or is it somthing else I am doing wrong? This is my code:  
Check components/Leaderboard.js

> **[GitHub - hristijankiko/fcc-leaderboard](https://github.com/hristijankiko/fcc-leaderboard)**
>
> Contribute to hristijankiko/fcc-leaderboard development by creating an account on GitHub.

These is my sorting function:  
sortByRecent(){  
let data = this.state.data;  
data.sort((a, b) =\> b.recent - a.recent);  
this.state.data = data;  
this.forceUpdate(); // Without this line the elements do not re-render  
}

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [November 15, 2016, 8:56pm UTC](https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714/2 "2016-11-15T20:56:07Z")

</div>

> Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable. [https://facebook.github.io/react/docs/react-component.html](https://facebook.github.io/react/docs/react-component.html)

This:

```auto
this.state.data = data;

```

is wrong!!!

You should use `this.setState({data})`

---

<div class="post-metadata">

**Author:** ![hristijankiko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hristijankiko/32/25293_2.png) [@hristijankiko](https://forum.freecodecamp.org/u/hristijankiko)\
**Post date:** [November 15, 2016, 9:02pm UTC](https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714/3 "2016-11-15T21:02:58Z")

</div>

That was it! That was the reason React did not re-render. I changed it to this.setState and now it doesn’t require the forceUpdate(). I saw this in the tutorials but I’m still not used to it and somtimes i forget to use it 🙂 Thanks for the help.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 18, 2021, 8:23am UTC](https://forum.freecodecamp.org/t/solved-react-does-not-re-render-after-state-change/55714/4 "2021-01-18T08:23:18Z")

</div>

Closed on January 18, 2021, 8:23am UTC


