prof-rossetti / internet-technologies

Internet Technologies (for Business)
Other
2 stars 72 forks source link

JavaScript Arrays - removing items #13

Open s2t2 opened 4 years ago

s2t2 commented 4 years ago

Translate this react code into a simple example:

   handleCategoryCheck(changeEvent){
        var category = changeEvent.target.value
        console.log("CHECK CATEGORY:", category)

        var userCategories = this.state.userCategories
        var categoryIndex = userCategories.indexOf(category) // will be -1 if item not in array
        if (categoryIndex >= 0 ) {
            userCategories.splice(categoryIndex, 1) // remove 1 item from array at the given position
        } else {
            userCategories.push(category)
        }

        console.log("CATEGORIES:", userCategories)
        this.setState({userCategories: userCategories}) // this is updating state but why not triggering a re-render?
    }