Next.js Discord

Discord Forum

What caused state change?

Answered
Sphecid wasp posted this in #help-forum
Open in Discord
Sphecid waspOP
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 Sphecid wasp
Looks like it was bad string
View full answer

5 Replies

Sphecid waspOP
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.
Sphecid waspOP
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)])
Sphecid waspOP
Looks like it was bad string
Answer
Sphecid waspOP
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