Skip to content

Commit

Permalink
docs: add docs for events
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikOseberg committed Jan 9, 2025
1 parent 87555d1 commit 10e5034
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ yarn add @unleash/proxy-client-react unleash-proxy-client

# How to use

This library uses the core [unleash-proxy-client](https://github.com/Unleash/unleash-proxy-client-js) JS client as a base.

## Initialize the client

Prepare [Unleash Proxy](https://docs.getunleash.io/reference/unleash-proxy) secret
Expand Down Expand Up @@ -134,6 +136,39 @@ const MyComponent = ({ userId }) => {
};
```

## Listening to events

The core Javascript client will emit different types on events based on what is happening in
the internals. You can listen on these events by leveraging the hook to return the client and listen
directly to these events. Or, if you are instantiating the FlagProvider with a client, use the client directly
to listen to events. [See the full list of events here.](https://github.com/Unleash/unleash-proxy-client-js?tab=readme-ov-file#available-events)

NOTE: `FlagProvider` is leveraging these internal events to provide information through `useFlagsStatus`.

```jsx
import { useUnleashContext, useFlag } from '@unleash/proxy-client-react';

const MyComponent = ({ userId }) => {
const client = useUnleashClient();

useEffect(() => {
if (client) {
const handleError = () => {
// Handle error
}

client.on('error', handleError)
}

return () => {
client.off('error', handleCallback)
}
}, [client])

// ...rest of component
};
```

# Advanced use cases

## Deferring client start
Expand Down

0 comments on commit 10e5034

Please sign in to comment.