How to filter data in both directions?
I have data that I receive from the server and store in the state, I need to filter it to open and close, but since I update the state I can only close it, but how can I then open it using only the state?
const data = [
{
id: 1,
title: "Project 01",
project_id: null,
is_project: true,
},
{
id: 2,
title: "Task 01",
project_id: 1,
is_project: false,
},
{
id: 3,
title: "Task 02",
project_id: 1,
is_project: false,
},
{
id: 4,
title: "Project 02",
project_id: null,
is_project: true,
},
{
id: 5,
title: "Task 03",
project_id: 4,
is_project: false,
},
];
const [state, setState] = useState([]);
useEffect(() => {
setState(data); // my fetch req
}, []);
const onVisible = (id) => {
const filtered = state?.filter((el) => el?.project_id !== id);
setState(filtered);
};
When I click on +, the tasks disappear, but if I click on + again they will not appear again, since I am using a state, how can I make them appear again?
2
Answers
this is a temporary solution , the whole mapping of the data part is a bit unclear .
You will have to save whether the values are expanded or not. Let say if you can modify the data before storing it into response then you can introduce a new key called
is_expanded
to indicated whether to show it as expanded.Initially, all of them are expanded. Once you toggle any project, any value that is part of it or the project will be toggled.