Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript Arrays - removing items #13

Open
s2t2 opened this issue Oct 12, 2020 · 0 comments
Open

JavaScript Arrays - removing items #13

s2t2 opened this issue Oct 12, 2020 · 0 comments

Comments

@s2t2
Copy link
Member

s2t2 commented Oct 12, 2020

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?
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant