My first intuition would be to pass directly the data to the context like so:
const AuthProvider = ({ children }: any) => {
const { data, error, loading } = useQuery(GET_USER_PROFILE)
if (loading) return null
if (error) return null
return (
<AuthContext.Provider value={data.user}>
{children}
</AuthContext.Provider>
)
}
But I see many examples over internet making use of useState() like so:
const AuthProvider = ({ children }: any) => {
const [user, setUser] = useState(null)
const { data, error, loading } = useQuery(GET_USER_PROFILE)
if (loading) return null
if (error) return null
if (data) {
setUser(data.user)
}
return (
<AuthContext.Provider value={user}>
{children}
</AuthContext.Provider>
)
}
I wonder why many example are introducing useState. Does it play a role in updating the context value such as forcing re-render?