Next.js Discord

Discord Forum

Rendered fewer hooks than expected

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
I have a situation where I have X number of cards. Cards can be created and deleted, and represent rows in a database, and so must be able to do individual update actions on themselves (For example, changing their name). For this, I have chosen to represent them as components with their own state. This is causing errors when I delete or add cards, though.
"Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement."
(it isn't caused by an early return afaik though I suppose anything is possible)

there's a component file called getCard that returns JSX html. In that function we have code that says:
let [cardState, setCardState] = useState({card:card, unsaved:false})

someFunctionThatHappensOnClick(){
  setCardState({card:{...card, name:"hi"},unsaved:true})
}
return <div>card properties here</div>

I am gathering that's not the right way to do it, I guess I'm hoping for an easy way to fix it without having to rewrite all the card's code because it's quite complicated

14 Replies

Northeast Congo LionOP
on delete, the parent does the equivalent of
setCardList(cardList.filter((item:CardItem) => item.id != card.id))

this is when the error happens - I've also used "Splice" to do the same thing
can you make a reproduction?
your code looks fine to me
Northeast Congo LionOP
It is certainly due to state management. The problem is, I was having state be assigned to components. It should be pretty easy to replicate it, but unfortunately I've pretty much fixed it on my end by doing something much more cumbersome. If it's important, I'll reproduce it, but if not I'd like to move on if that's alright.
@Northeast Congo Lion It is certainly due to state management. The problem is, I was having state be assigned to components. It should be pretty easy to replicate it, but unfortunately I've pretty much fixed it on my end by doing something much more cumbersome. If it's important, I'll reproduce it, but if not I'd like to move on if that's alright.
Yeah making a reproduction is usually quite hard for complex codebases, but from our point of view, we only see a few lines of code all of which look fine to us, so how can we help then… that’s why a reproduction is necessary, else you will have to debug (or ignore the bug) on your own. See [here](https://nextjs-discord-common-questions.joulev.dev/how-to-create-a-minimal-reproduction-repository) for how I usually make reproduction for bugs in complex codebases
Northeast Congo LionOP
I see I see
I'm 99% sure the answer is going to be "Well you're not supposed to do it that way" if I did the full reproduction. And I've since fixed the problem by doing something really weird.
But again, if it sounds like something that might be important for other users, I'll build a reproduction environment.
Hmm no need to force yourself. If you fixed it already, then even if the fix is bad, what works will work, and if you don’t have much time for example, you should probably prioritise more important parts of your app first. That said if you want to get help on how to solve it the right way, then yeah please do make a reproduction, that will help us a lot to know what’s wrong and potentially someone who knows how to fix might come and explain it to you
Northeast Congo LionOP
For sure - I may come back to this once the big bug I'm having is fixed...
this is what's killing me
it's making my app unusable in production but fine in dev
which is the worst situation