i want to filter the elements that I get on the page, based on the checkbox value(if it's checked or not). For that i have the following code:
import React from 'react'
import CocktailCard from './CocktailCard'
import { useGlobalContext } from '../helpers/context'
export default function Cocktails() {
const isAlcoholic = React.useRef(null);
const {cocktails} = useGlobalContext()
const changeFilter = (isAlcoholic) ? cocktails.filter(cocktail => cocktail.isAlcoholic === "Alcoholic") : cocktails
console.log(changeFilter)
if(cocktails.length < 1) {
return <h2 className="section-title">No cocktails available</h2>
}
return (
<div>
<div className="row">
<div className="form-check">
<label className="form-check-label" htmlFor="alcoholic">
Alcoholic
<input
className="form-check-input"
type="checkbox"
ref={isAlcoholic}
onChange={() => {
changeFilter(isAlcoholic.current.checked);
}}/>
</label>
</div>
</div>
<div className="container">
<div className="row">
{
cocktails.map((item) => {
return <CocktailCard key={item.id} {...item}/>
})
}
</div>
</div>
</div>
)
}
I tried to do changeFilter function so it will filter out the alcoholic one, if not, it will give back the current array. I'm sure I do something wrong, because I get an error in the console saying that changeFilter is not a function.
Can you guys give me a better idea how I should tackle this error and logic in general?