0

I added a checkbox to my rendered HTML of my react.

When checked, I want it to fire a function called handleChange.

That handleChange function will take the value of the checkbox and then use that value to filter an array called bookshelfsArray.

But when I click the checkbox, I see an error in the console:

filterStore is undefined

But I set the value of filterStore to "360" when I'm testing, so it should be fine.

Is there anything else I need to set for this to work?

Thanks!

Here is my full code file I am testing:

import React from 'react';
import { theLibraryData } from 'data';

function bookshelfRow(props) {
return (
        <div>
            <div>{title}</div>
        </div>
        )

}

export default class bookshelfDemo extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            bookshelfs: theLibraryData,
            filterStore: 0
        };
    }

    handleChange = e => {
        const { value } = e.target;
        console.log("You checked the checkbox!");
        this.setState({filterStore:value})
    }

    render() {

        let bookshelfsArray = this.state.bookshelfs;

        bookshelfsArray = bookshelfsArray.filter(x => x.storeId === filterStore);

        return (
            <div>
                <input type="checkbox" name="test" onChange={this.handleChange} value="360" />
                {bookshelfsArray.map((bookshelf) => <bookshelfRow key={bookshelf.id} bookshelf={bookshelf} />)}
            </div>
        );
    }

}

2 Answers 2

1

You're issue has to do with the fact that you are referencing filterStore versus what I think you want to reference this.state.filterStore, located on this line:

bookshelfsArray = bookshelfsArray.filter(x => x.storeId === filterStore);

Since the variable filterStore has never been assigned, it is undefined. Fix it like this:

bookshelfsArray = bookshelfsArray.filter(x => x.storeId === this.state.filterStore);

Additionally, you should know that they way you are using setState in the line below:

this.setState({ filterStore: value })

Actually sets your library data to undefined, since when you update the state object, this field is cleared. You can fix this with the following:

this.setState({ filterStore: value, ...this.state })
Sign up to request clarification or add additional context in comments.

10 Comments

Hi! I am not sure this is working because when I click the checkbox nothing happens. I put a console.log in the handleChange and I do see that getting fired. But the page is never refreshed after the checkbox click
this.setState({ filterStore: value }) doesn't set library to undefined, it will update only filterStore. And this.setState({ filterStore: value, ...this.state }) is incorrect, this way this.state will overwrite filterStore: value with its filterStore property value. the correct way to create a new object with new values is spreading the old object first.
your original is correct actually, SetState doesn't need to return the whole state object. But if it needed that would be the correct way. you can do this.setState({ ...this.state, filterStore: value }) or this.setState({ filterStore: value }), the outcome will be the same
@SkyeBoniwell is x.storeId correct, or should it be x.id? it could be that you are comparing string with number as you say...
@buzatto Yep, it was a type issue...this fixed it! x.storeId === parseInt(this.state.filterStore)
|
1

you should use :

bookshelfsArray = bookshelfsArray.filter(x => x.storeId === this.state.filterStore);

1 Comment

Please explain why this might resolve the OP's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.