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>
);
}
}