D
D
Dmitry Samoilov2021-12-19 20:59:02
React
Dmitry Samoilov, 2021-12-19 20:59:02

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

The array itself looks like this:
[
  {
    "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
  }
]

The usual array, which consists of objects with id

I display posts using map and in the loop I tried to add a function with a filter to checked, which would look for matching id and if there are, then set it to checked true, but after this function checked true works on all posts. I don’t really understand what the problem is, why all posts became checked true
//код вне цикла
const setChecked = (idS) => {
   bookmarksData.filter((e) => e.id === idS) ? true : false
 }

//код в цикле, где я вывожу посты
<Checkbox
                  icon={<BookmarkBorderIcon />}
                  checkedIcon={<BookmarkIcon />}
                  checked={() => setChecked(id)}
                  onClick={() => savedBokmark({ id })}
 />

61bf71cb17201749263374.jpeg

Can you tell me what the problem is and how to make the checkbox work normally with filtering?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Samoilov, 2021-12-19
@distCom

It seems I understood what the problem was, I solved it through find, not filter, it seems to be working fine now

D
D', 2015-07-28
@littleguga

And what does PHPStorm have to do with it if it needs to be done with Gulp/Grunt?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question