React component not re-rendering when updating state
<p>I think you have a better solution to this problem:
I have a file containing an array of my cat objects: </p>
<pre class="brush:php;toolbar:false;">var categories = [
{
"id": 1,
"name" : "Faktury",
"selected" : false
},
{
"id": 2,
"name" : "Telefony",
"selected" : false
},
{
"id": 3,
"name" : "Komputery",
"selected" : false
},
{
"id": 4,
"name" : "Rachunkowośc",
"selected" : false
},
{
"id": 5,
"name" : "Finanse",
"selected" : false
}
];</pre>
<p>I have: </p>
<pre class="brush:php;toolbar:false;"><ul className="category">
{this.state.categories.map((item,index) =>
<li onClick={()=>this.filterCategory(item,index)} key={item.id} className={item.selected? 'active' : ''}>{item.name}< /li>
)}
</ul></pre>
<p>My filterCategory function:</p>
<pre class="brush:php;toolbar:false;">filterCategory(item,index) {
this.state.categories[index].selected = !this.state.categories[index].selected;
this.forceUpdate();
}</pre>
<p>Do you know how I can implement this without using <code>forceUpdate()</code>? I read on Stack that using <code>this.forceUpdate()</code> should be avoided. </p>