Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jinfa-platform
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
linweijiong
jinfa-platform
Commits
47d53bc7
Commit
47d53bc7
authored
Jul 12, 2021
by
XieZhiXiong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加 内部请求详情HOC
parent
5206453f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
fetchDetailHoc.tsx
src/pages/member/common/hoc/fetchDetailHoc.tsx
+54
-0
No files found.
src/pages/member/common/hoc/fetchDetailHoc.tsx
0 → 100644
View file @
47d53bc7
/*
* @Author: XieZhiXiong
* @Date: 2021-07-12 14:21:53
* @LastEditors: XieZhiXiong
* @LastEditTime: 2021-07-12 17:27:33
* @Description: 获取详情 hoc,要求传入的组件接受 dataSource、loading 属性
*/
import
React
,
{
useState
,
useEffect
}
from
'react'
;
export
interface
IConfig
<
P
>
{
/**
* 请求详情方法
*/
fetchDetail
:
()
=>
Promise
<
{
data
:
P
}
>
;
/**
* fetch callback
*/
fetchCallback
?:
(
data
:
P
)
=>
void
;
}
const
FetchDetailHoc
=
<
P
,
T
extends
{}
>
(config: IConfig
<
P
>
, WrapComponent: React.ComponentType
<
T
>
) =
>
{
const
{
fetchDetail
,
fetchCallback
}
=
config
;
const
[
detail
,
setDetail
]
=
useState
<
P
>
();
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
getDetail
=
()
=>
{
if
(
fetchDetail
)
{
setLoading
(
true
);
fetchDetail
().
then
((
res
)
=>
{
setDetail
(
res
.
data
);
fetchCallback
?.(
res
.
data
);
}).
finally
(()
=>
{
setLoading
(
false
);
});
}
};
useEffect
(()
=>
{
getDetail
();
},
[]);
return
React
.
useMemo
(()
=>
{
return
(
props
:
Omit
<
T
,
(
'dataSource'
|
'loading'
)
>
):
JSX
.
Element
=>
{
return
(
<
div
>
<
WrapComponent
{
...
props
as
any
}
dataSource=
{
detail
}
loading=
{
loading
}
/>
</
div
>
);
}
},
[
WrapComponent
,
detail
,
loading
]);
}
;
export default FetchDetailHoc;
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment