Replies: 5 comments 3 replies
-
React does not allow you to render hooks conditionally. You have to always render the same hooks on every render. The There aren't many elegant solutions (that I know of) for this, I usually do one of the following:
So, yeah, make sure the same hooks are rendered on every component render until it unmounts. You are able to play as much as you want with the hook instances, but you you can't conditionally render or not render them. |
Beta Was this translation helpful? Give feedback.
-
@mjangir We have a similar requirement. Would you be willing to share your custom hooks for rendering radio buttons? |
Beta Was this translation helpful? Give feedback.
-
For those looking for a solution, here is something I made based on a suggestion by @mjangir 👍 and forked from
|
Beta Was this translation helpful? Give feedback.
-
The most important thing I've learned about hooks is to always offer an |
Beta Was this translation helpful? Give feedback.
-
I know this is an old thread, but I often use a key to force react to see it as a new component when I have something that needs to change the number of hooks. Not the most performant maybe, but a nice escape hatch.
|
Beta Was this translation helpful? Give feedback.
-
It's nice that react table provides
useRowSelect
plugin hook to maintain selected rows where we can select/unselect multiple table records. My requirement is slightly different. In our table, we want to render radio buttons instead of checkbox and user can select only one row at a time.I achieved this by creating new plugin hooks called
useSingleRowSelect
anduseRadioColumn
. The first hook maintains only one selected row in the state and the other hook just injects one column with radio button in the beginning of the columns array.This is how I use it:
const instance = useTable(...table opts, ...otherhooks, props.multi ? useRowSelect : useSingleRowSelect)
The problem here is if I just change the prop
multi
to false, the component re-renders and throws an errorrendered more hooks than during the previous render
.Does anybody know how to solve this error?
Beta Was this translation helpful? Give feedback.
All reactions