I have a block of code like this:
randomArray(arrayOfImage){
//algorithm goes here
return shuffledArray;
}
shuffle(){
this.randomArray(images);
this.forceUpdate();
}
render(){
let array = this.randomArray(images); //images is a declared array
let squares = array.map((item,index) => <Squares/> )
return(
<div>
<div id='square-container'>
{squares}
</div>
<button className='btn' onClick = {this.shuffle.bind(this)}>Shuffle</button>
</div>
)
}
Basically, I have an array images
declared. The function randomArray()
return a shuffled version of images
. And then, for each of the item inside the shuffled array array
the browser will render a div with the given props.
Now I have a button so that the user can shuffle the array themselves. Here, I use forceupdate()
because even though the array is shuffled when the button is clicked, the DOM won't update because there is no changes in the state.
It works! But since using forceupdate()
is not encouraged, how should I make this less... say, amateur?