Skip to content

Commit

Permalink
added use api
Browse files Browse the repository at this point in the history
  • Loading branch information
seeden committed Mar 25, 2019
1 parent d48690b commit 0ca3ab8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/hooks/useApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @flow
import { useContext, useState } from 'react';
import FacebookContext from '../FacebookContext';

export default (onReady?: Function) => {
const {
handleInit,
api,
} = useContext(FacebookContext);

useState(async () => {
const api2 = await handleInit();
if (onReady) {
onReady(api2);
}
});

return [api, handleInit];
};
9 changes: 5 additions & 4 deletions src/hooks/useShare.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @flow
import { useContext } from 'react';
import clearUndefinedProperties from '../utils/clearUndefinedProperties';
import FacebookContext from '../FacebookContext';
import useApi from './useApi';

export default () => {
const { api } = useContext(FacebookContext);
const [, prepareApi] = useApi();

function handleShare(options: Object) {
async function handleShare(options: Object) {
const {
href,
display,
Expand All @@ -17,6 +16,8 @@ export default () => {
...rest
} = options;

const api = await prepareApi();

return api.ui(clearUndefinedProperties({
method: 'share',
href,
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export Fields from './constants/Fields';
export FacebookContext from './FacebookContext';

export useShare from './hooks/useShare';
export useApi from './hooks/useApi';

0 comments on commit 0ca3ab8

Please sign in to comment.