Skip to content
Advertisement

How can I set the default state of a checkbox based on value in firestore in react table

How can I set the default state of checkboxes based on values from firestore in React Table. Sorry if the question is a bit basic. I have read through the official docs and couldn’t really make sense of how to do it, I’ve also looked at a good few other examples.

I’m looking to check the value in a firestore document when the page loads, then set the default value of the checkbox. I also want the user to be able to check/uncheck it which will then update that field in the doc.

I’ve looked at this initialState.selectedRowIds: Object<rowId: Boolean> in the docs but cant figure out how to get it to work or what else needs to be connected with it.

Any help would be greatly appreciated.

Here is my implementation below.

JavaScript

Advertisement

Answer

Although maybe not the most recommended way.

This is whats worked for me:

I read in a boolean from firestore, then pass it to the checkbox. Whenever I check/uncheck the checkbox I have a function that toggles the value in firestore, and I have a snapshot function that’s subscribed to any changes so automatically updates the actual checkbox.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement