I want to have options where if a user chooses 'all' it should display the whole array, if 'active' then only items in array that are not checked, and 'complete' for all checked values.
But, I don't get the whole array when switching between options. For example, if I choose 'active', then 'complete' and then 'all', at the end nothing will show because after switching array is empty.
I tried to copy existing array in temporary state and then manipulate with it, but not working.
This is how it looks like:
const [todo, dispatch] = useReducer((state: any, action: any): any => {
switch (action.type) {
//...
case 'SELECT':
switch (action.option) {
case 'all':
const temp1 = [...state]
return temp1
case 'complete':
const temp2 = [...state]
return temp2.filter((i: any) => i.isCheck === true)
case 'active':
const temp3 = [...state]
return temp3.filter((i: any) => i.isCheck === false)
default:
return state
}
}
}, [])
<select onChange={(e: any) => dispatch({ type: 'SELECT', option: e.target.value })}>
<option value="choose" selected>Choose</option>
<option value="all">All</option>
<option value="active">Active</option>
<option value="complete">Complete</option>
</select>
And here is whole component : https://pastebin.com/Xr1gm4dV
Any solutions or ideas?
EDIT:
const [options, setOptions] = useState([{}])
const [todo, dispatch] = useReducer((state: any, action: any): any => {
setOptions([...state])
switch (action.type) {
//...
case 'SELECT':
switch (action.option) {
case 'all':
setOptions([...state])
return options
case 'complete':
setOptions([...state])
return options.filter((i: any) => i.isCheck === true)
case 'active':
setOptions([...state])
return options.filter((i: any) => i.isCheck === false)
default:
return state
}
}
})