我正在尝试找到一种useFecthApi
有条件地使用的正确方法。
我有以下组件:
export const DetailedUser: FC = () => {
const userState = useFetchApi(() => findUser(userId))
const fetchContacts = mustFecthContacts() // implemenattion does not matter
return (
<>
<UserInformation userState={userState } />
</>
)
}
type Props = Readonly<{
userState: State<UserDetails>
}>
export const UserInformation : FC<Props> = ({ userState }) => {
}
我想做的是创建一个仅当等于的contactState
定义,然后将其传递给.const contactState= useFetchApi(() => findContacts(userId))
fetchContacts
true
contactState
UserInformation
所以基本上,类似:
export const DetailedUser: FC = () => {
const userState = useFetchApi(() => findUser(userId))
// fetchContacts is a boolean (implementation is not important)
const fetchContacts = mustFecthContacts()
const contactStates = fetchContacts ? useFetchApi(() => findContacts(userId)) : undefined
return (
<>
<UserInformation userState={userState} contactsState = {contactStates } />
</>
)
}
type Props = Readonly<{
userState: State<UserDetails>,
contactStates? : State<ContactDetails>
}>
export const UserInformation : FC<Props> = ({ userState, contactStates }) => {
}
我对做出反应(以及对叶状体的发展)还很陌生,所以我不知道如何正确地实现这一点。有什么建议吗?
谢谢。