Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Atom from Redux-Middleware #1696

Closed
SmashPlusUltra opened this issue Mar 25, 2022 · 1 comment
Closed

Update Atom from Redux-Middleware #1696

SmashPlusUltra opened this issue Mar 25, 2022 · 1 comment

Comments

@SmashPlusUltra
Copy link

New here, trying to migrate somethings from redux to recoil.
Is there a way to update a Atom from a Midleware?, the problem" Hooks can only be called inside of the body of a function component.", the code is more or less like..

//Define my atomFamily from a selector fetching an API
export const instrumentState = atomFamily({
    key: 'instrument',
    default: selectorFamily({
        key: "listAtom/Default",
        get: (id) => ({ get }) => {
            const d = useRecoilValue(instrumentsState)
            return  d[id] 
        }
    })
})

export const signalRMiddleware = storeAPI => next => action => {        
        switch (true) {
        case startConnection.match(action): {   
                startWebSocket(); //Start the connection...
                hubConnection.on("ReceiveMessage", (user, data, channel) => {               
                switch (channel) {
                    case 'TypeOfMessage':
                        {
                            //For each message I recieved I try to modify the atom, it's hardcoded just for testing                           
                                const [instrument, setInstrument] = useRecoilState(instrumentState(10))
                                setInstrument({ ...instrument, price: 9 })                            
                        }
                }
          }
       }
}
@SmashPlusUltra
Copy link
Author

My bad, I was using the hook in a wrong way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant