Skip to content

Commit

Permalink
Use useLayoutEffect when calling .setOptions() in ChannelProvider
Browse files Browse the repository at this point in the history
This is a temporary fix for #1704. This does not fix the underlying
problem of ably-js sending two `ATTACH` messages with the same options
when we call `.setOptions()` in `ChannelProvider`.

What `useLayoutEffect` accomplishes is it runs before children
components' `useEffect` hooks, thus it sets options for a channel before
attachment process starts, so there will be no need for reattachment
and sending a second `ATTACH` message.
  • Loading branch information
VeskeR committed Mar 20, 2024
1 parent af0f3f1 commit 54487f7
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/platform/react-hooks/src/ChannelProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo } from 'react';
import React, { useLayoutEffect, useMemo } from 'react';
import * as Ably from 'ably';
import { type AblyContextValue, AblyContext } from './AblyContext.js';
import { channelOptionsWithAgent } from './AblyReactHooks.js';
Expand Down Expand Up @@ -44,7 +44,7 @@ export const ChannelProvider = ({
};
}, [derived, client, channel, channelName, _channelNameToChannelContext, ablyId, context]);

useEffect(() => {
useLayoutEffect(() => {
channel.setOptions(channelOptionsWithAgent(options));
}, [channel, options]);

Expand Down

0 comments on commit 54487f7

Please sign in to comment.