My current context with a method to set the updated context looks like this:
export const AppContextProvider = ({ children }) => {
const [currentAppContext, setCurrentAppContext] = useState(initApp)
const setAppContext = values => {
setCurrentAppContext(values)
}
return <AppContext.Provider value={{ appContext: currentAppContext, setAppContext }}>{children}</AppContext.Provider>
}
and I set the new context in a component like so:
export default function Settings() {
const { appContext, setAppContext } = useContext(AppContext)
const func = ({ accountId }) => {
setAppContext({
...appContext,
accountId: accountId,
})
},
})
return <></>
}
But I would prefer to do just setAppContext((currentContext) => ({...currentContext, accountId: accountId}))
like we do in the useReducer
hook useReducer(state => state.item)
kind of thing. Is this possible, if so, how would I write it?