我有 useUserData 钩子,用于从服务器获取用户数据
function App({ client }) {
const data = useUserData({ client })
console.log(data)
// logs {username: 'foo', email: 'foo@bar.com'}
return <AppLayout feed={<Feed />} />
}
export default withApollo(App)
钩子本身是这样的:
function useUserData({ client }) {
const [state, setState] = useState(null)
const getUserData = async () => {
const {
data: { me }
} = await client.query({
query: GET_ME
})
const { username, email } = me
setState(me)
}
useEffect(() => {
getUserData()
}, [])
return state
}
export default useUserData
我看到我必须为每个组件使用HOC withApollo 。这是一个好方法吗?