Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/CherryProjects/react-facebook

Facebook components like a Login button, Like, Share, Chat, Comments, Page or Embedded Post
https://github.com/CherryProjects/react-facebook

facebook facebook-api facebook-components javascript react react-components

Last synced: 4 months ago
JSON representation

Facebook components like a Login button, Like, Share, Chat, Comments, Page or Embedded Post

Lists

README

        

# React Facebook Components

[![NPM version][npm-image]][npm-url]

[npm-image]: https://img.shields.io/npm/v/react-facebook.svg?style=flat-square
[npm-url]: https://www.npmjs.com/react-facebook
[travis-image]: https://img.shields.io/travis/seeden/react-facebook/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/seeden/react-facebook
[coveralls-image]: https://img.shields.io/coveralls/seeden/react-facebook/master.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/seeden/react-facebook?branch=master
[github-url]: https://github.com/seeden/react-facebook
[support-url]: https://github.com/sponsors/seeden

# Components

- Facebook provider (provide settings to child components)
- Login button (provide user profile and signed request)
- Like button
- Share and Share button
- Comments
- Comments count
- Embedded post
- Embedded video
- Page
- Feed
- Group
- Message Us
- Customer Chat
- Save
- Status
- Share
- Subscribe
- User Profile

# Support

[Become my sponsor][support-url] and help me maintain this project. Thank you

# Initialisation

By default FacebookProvider is loading facebook script immediately after render (you are able to use it with SSR).
If you want to download facebook script only when facebook component is rendered you need to add parameter lazy to FacebookProvider.
Use only one instance of the FacebookProvider on your page.

# Usage

## Like button

```tsx
import { FacebookProvider, Like } from 'react-facebook';

export default function LikeExample() {
return (



);
}
```

## useShare

```tsx
import { FacebookProvider, useShare } from 'react-facebook';

export default function ShareExample() {
const { share, isLoading, error } = useShare();

async function handleShare() {
await share({
href: 'http://www.facebook.com',
});
}

return (
Share
);
}
```

## Share button

You can use predefined button

```tsx
import { FacebookProvider, ShareButton } from 'react-facebook';

export default function ShareButtonExample() {
return (


Share


);
}
```

## Comments

```tsx
import { FacebookProvider, Comments } from 'react-facebook';

export default function CommentsExample() {
return (



);
}
```

If comments do not work and you are seeing this error in the browser console:

`Refused to display 'https://www.facebook.com/v3.1/plugins/comments.php?blahblahblah' in a frame because it set 'X-Frame-Options' to 'DENY'.`

Possible reasons:
1. If the site visitor is from the EU region, the visitor needs to be **both**:
- Logged in to Facebook.
- Have third-party cookies enabled in FB privacy settings.
2. User is using a browser that is blocking third party cookies by default (for example Safari and Firefox).

It is not a bug in this library, [there is no way around it](https://developers.facebook.com/support/bugs/854001531875629/).

## Comments count

```tsx
import { FacebookProvider, CommentsCount } from 'react-facebook';

export default function CommentsCountExample() {
return (



);
}
```

## useLogin

```tsx
import { FacebookProvider, useLogin } from 'react-facebook';

export default function LoginExample() {
const { login, status, isLoading, error} = useLogin();

async function handleLogin() {
try {
const response = await login({
scope: 'email',
});

console.log(response.status);
} catch (error: any) {
console.log(error.message);
}
}

return (

Login via Facebook

);
}
```

### LoginButton

```tsx
import { FacebookProvider, LoginButton } from 'react-facebook';

export default function LoginButtonExample() {
functon handleSuccess(response) {
console.log(response.status);
}

function handleError(error) {
console.log(error);
}

return (


Login via Facebook


);
}
```

## Embedded post

```tsx
import { FacebookProvider, EmbeddedPost } from 'react-facebook';

export default function EmbeddedPostExample() {
return (



);
}
```

## Page

```js
import React, { Component} from 'react';
import { FacebookProvider, Page } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## Feed

```js
import React, { Component} from 'react';
import { FacebookProvider, Feed } from 'react-facebook';

export default class Example extends Component {
render() {
return (


{({ handleClick }) => (
Share on Feed
)}


);
}
}
```

## Group

```js
import React, { Component } from 'react';
import { FacebookProvider, Group } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## MessageUs

```js
import React, { Component} from 'react';
import { FacebookProvider, MessageUs } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## SendToMessenger

```js
import React, { Component} from 'react';
import { FacebookProvider, SendToMessenger } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## MessengerCheckbox

```js
import React, { Component} from 'react';
import { FacebookProvider, MessengerCheckbox } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## CustomChat

```js
import React, { Component} from 'react';
import { FacebookProvider, CustomChat } from 'react-facebook';

export default class Example extends Component {
render() {
return (



);
}
}
```

## API Access

```tsx
import { FacebookProvider, useFacebook } from 'react-facebook';

export default function UseFacebookExample() {
const { isLoading, init, error } = useFacebook();

async function handleClick() {
const api = await init();

const response = await api.login();
const FB = await api.getFB(); // Get original FB object
}

return (

Login

);
}
```

## useSubscribe - Subscribe to events

```tsx
import { FacebookProvider, useSubscribe } from 'react-facebook';

export default function UseSubscribeExample() {
const latestValue = useSubscribe('auth.statusChange', (value) => {
console.log('new response', value);
});

return (


{latestValue}

);
}
```

## useLoginStatus - read login status of current user

```tsx
import { FacebookProvider, useLoginStatus } from 'react-facebook';

export default function UseLoginStatusExample() {
const { status, isLoading, error } = useLoginStatus();

return (


Current user login status: {isLoading ? 'Loading...' : status}

);
}
```

## User Profile

This component will not sign user. You need to do that with another component.
Default scope: 'id', 'first_name', 'last_name', 'middle_name', 'name', 'name_format', 'picture', 'short_name', 'email'.
If profile is undefined login status !== LoginStatus.CONNECTED

```tsx
import { FacebookProvider, useProfile } from 'react-facebook';

export default function UseProfileExample() {
const { profile, isLoading, error } = useProfile(['id', 'name']);

return (


{profile?.name} has id: {profile?.id}

);
}
```

# Testing

```sh
npx cypress open --component
```

# Support

[Become my sponsor][support-url] and help me maintain this project. Thank you

## Credits

[Zlatko Fedor](http://github.com/seeden)