Answer the question
In order to leave comments, you need to log in
How to control checkbox with filtering?
Hello, I want to make a managed checkbox that turns checked into true if the post id matches the id from another array, but I don't understand how to do it.
When you click on the checkbox, the post id is added to the bookmarks array, so
I want to make it controllable.
const bookmarksData = useSelector((state) => state.bookmark.bookmarksData);
[
{
"id": 5
},
{
"id": 2
},
{
"id": 19
},
{
"id": 22
},
{
"id": 23
},
{
"id": 21
},
{
"id": 25
},
{
"id": 35
},
{
"id": 22
},
{
"id": 12
},
{
"id": 27
},
{
"id": 10
},
{
"id": 10
},
{
"id": 5
},
{
"id": 1
},
{
"id": 1
}
]
//код вне цикла
const setChecked = (idS) => {
bookmarksData.filter((e) => e.id === idS) ? true : false
}
//код в цикле, где я вывожу посты
<Checkbox
icon={<BookmarkBorderIcon />}
checkedIcon={<BookmarkIcon />}
checked={() => setChecked(id)}
onClick={() => savedBokmark({ id })}
/>
Answer the question
In order to leave comments, you need to log in
It seems I understood what the problem was, I solved it through find, not filter, it seems to be working fine now
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question