Next.js Discord

Discord Forum

What caused state change?

Answered
Large oak-apple gall posted this in #help-forum
Open in Discord
Large oak-apple gallOP
Component has one state (context).
On initial render it should do some checks and update state, then rerender with correct state. It derives value from other unchanged value, so it should run this update and rerender 1 time at most.
And it does that. But then state goes previous value.

Is there easy way to track history of state changes and see who updates it?
Answered by Large oak-apple gall
Looks like it was bad string
View full answer

5 Replies

Large oak-apple gallOP
And more importantly, trying to intercept it by useefect with its value as dependency is not working.

It triggers for first, valid update. But for second ninja update it is not triggered, even though value changes
Carpenter wasp
Without any code or example project it's very hard to determine what the issue might be or if it's bug, or some simple misconception.
Large oak-apple gallOP
This is part where I intercept state change. Where is thing which bypasses it is my main question
useEffect(() => {
    const currentSelectedOptions = Object.entries(
        quizState.selectedOptions || {},
    )
    const allowedSelectedOptions = currentSelectedOptions.filter(([id]) =>
        filteredOptionsIds.includes(id),
    )

    updateQuizState({
        selectedOptions: Object.fromEntries(allowedSelectedOptions),
    })
}, [String(filteredSelectedOptions), String(quizState.selectedOptions)])
Large oak-apple gallOP
Looks like it was bad string
Answer
Large oak-apple gallOP
Both String() and toString() give object object thing. Now I wonder how does my project even work correctly since I used both in many places