Next.js Discord

Discord Forum

States not updating for functions but for useEffect()

Unanswered
Devils King posted this in #help-forum
Open in Discord
I have two states I have created using useReducer hook, which I am updating on key press, one is typeData and other is systemData.

On first key press, the typeData is updated and updatedValues are consoled in useEffect() with typeData dependency but any function I call after update is still showing old typeData values.

This is how I created reducers
const [typeData, typeDispatch] = useReducer(mainReducer, {});
const [systemData, systemDispatch] = useReducer(systemReducer, {});

This is my main reducer.
function mainReducer(state, action)
    {
        switch(action.type){
            case 'overallUpdate':
                console.log('Overall Update Called!', action.data)
                return action.data;
            default:
                console.log(`Action type: ${action.type} not expected!`)
                return state;
        }
    }


I am updating a array in typeData with three values to later have only one value
typeDispatch({type:"overallUpdate", data:{lanes:rPos,complete:nComplete, incomplete:nIncomplete,currentLane:-1, aKeys:nKeys, extraText:nExtraText}});


Updating value correctly showing in console with useEffect():
 useEffect(()=>{console.log("Type Data updated!"+a++,typeData.aKeys);},[typeData])


Old values are again shown even when the following function is called after state update with event keyup
function removeKeyFromSystem(e)
    {
        if(!systemData.keyPressed) return;
        if(!systemData.keyPressed.includes(e.key)) return;
        let copData = {...typeData};
        console.log("Copied Data "+a+" ",copData);
        systemDispatch({type:"removeKey", data:e.key});
    }

7 Replies

Let me know if extra information needed.
@B33fb0n3 really sorry for the ping, can you help me? Really stuck with this issue 😞 No worries if you can't
@linesofcode sorry for the ping :(
It’s rude and against the rules of this discord to ping people not involved in your conversation.
Imagine if everyone who needed help pinged people and then said “sorry”
I am sorry! I won't ping now.
Fixed nvm