Skip to content

Commit 0ca3ab8

Browse files
committed
added use api
1 parent d48690b commit 0ca3ab8

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/hooks/useApi.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @flow
2+
import { useContext, useState } from 'react';
3+
import FacebookContext from '../FacebookContext';
4+
5+
export default (onReady?: Function) => {
6+
const {
7+
handleInit,
8+
api,
9+
} = useContext(FacebookContext);
10+
11+
useState(async () => {
12+
const api2 = await handleInit();
13+
if (onReady) {
14+
onReady(api2);
15+
}
16+
});
17+
18+
return [api, handleInit];
19+
};

src/hooks/useShare.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// @flow
2-
import { useContext } from 'react';
32
import clearUndefinedProperties from '../utils/clearUndefinedProperties';
4-
import FacebookContext from '../FacebookContext';
3+
import useApi from './useApi';
54

65
export default () => {
7-
const { api } = useContext(FacebookContext);
6+
const [, prepareApi] = useApi();
87

9-
function handleShare(options: Object) {
8+
async function handleShare(options: Object) {
109
const {
1110
href,
1211
display,
@@ -17,6 +16,8 @@ export default () => {
1716
...rest
1817
} = options;
1918

19+
const api = await prepareApi();
20+
2021
return api.ui(clearUndefinedProperties({
2122
method: 'share',
2223
href,

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ export Fields from './constants/Fields';
4141
export FacebookContext from './FacebookContext';
4242

4343
export useShare from './hooks/useShare';
44+
export useApi from './hooks/useApi';

0 commit comments

Comments
 (0)