React Compiler
Import call state hooks directly to be compatible with React Compiler's static analysis requirements.
Best Practices
- Import call state hooks by name, for example
import { useParticipants } from "@stream-io/video-react-sdk". - Do not call
useCallStateHooks()inside a component. - If you keep
useCallStateHooks(), call it once at module scope under an alias. - Use
@stream-io/video-codemodto move existinguseCallStateHooks()calls to module scope.
Problem
When using React Compiler, calling useCallStateHooks() inside a component violates React Compiler's static analysis requirements. These requirements are enforced during development by ESLint, which reports the error shown below.
ESLint: Hooks must be the same function on every render, but this value may change over time to a different function.
See https://react.dev/reference/rules/react-calls-components-and-hooks#dont-dynamically-use-hooks (react-compiler/react-compiler)Why this breaks with React Compiler
useCallStateHooks() is not a React hook. It is a factory function that returns hooks.
Calling it inside a component creates hook function references during render. Even if those references are stable, React Compiler cannot verify that at compile time. Because hook functions must be statically known and identical on every render, this pattern is rejected.
Solution
Every call state hook is exported directly from the SDK. Import the hooks you need by name and remove the useCallStateHooks() call. The hook names, arguments and return values stay the same.
Before / After
Before:
import { useCallStateHooks } from "@stream-io/video-react-sdk";
const MyComponent = () => {
const { useCallCallingState, useParticipants } = useCallStateHooks();
const callingState = useCallCallingState();
const participants = useParticipants();
return (
<div>
<div>State: {callingState}</div>
<div>Participants: {participants.length}</div>
</div>
);
};After:
import {
useCallCallingState,
useParticipants,
} from "@stream-io/video-react-sdk";
const MyComponent = () => {
const callingState = useCallCallingState();
const participants = useParticipants();
return (
<div>
<div>State: {callingState}</div>
<div>Participants: {participants.length}</div>
</div>
);
};Alternative: call useCallStateHooks() at module scope
If you prefer to keep useCallStateHooks(), call it once at module scope, then destructure the hooks you need. Use those hooks normally inside your components.
- Alias the import to avoid naming confusion (import as
getCallStateHooks) - Call the factory once at module scope
- Destructure hooks at the top level
- Use the hooks normally inside components
import { useCallStateHooks as getCallStateHooks } from "@stream-io/video-react-sdk";
const { useCallCallingState, useParticipants } = getCallStateHooks();
const MyComponent = () => {
const callingState = useCallCallingState();
const participants = useParticipants();
return (
<div>
<div>State: {callingState}</div>
<div>Participants: {participants.length}</div>
</div>
);
};What not to do
Do not call the factory inside the component:
import { useCallStateHooks as getCallStateHooks } from "@stream-io/video-react-sdk";
const MyComponent = () => {
const hooks = getCallStateHooks(); // still dynamic, still triggers the error
const { useCallCallingState } = hooks;
// ...
};Why alias to getCallStateHooks
Aliasing useCallStateHooks to getCallStateHooks is required. If you call useCallStateHooks() at module scope without aliasing, ESLint will error because functions starting with use are treated as hooks and cannot be called outside of components. The alias makes it clear that you're calling a factory function at module scope, not using a hook inside a component.
Automated migration with codemod
The @stream-io/video-codemod package transforms your codebase automatically. Its use-call-state-hooks codemod applies the module scope pattern described above.
Usage
Run the use-call-state-hooks codemod on your codebase:
npx @stream-io/video-codemod use-call-state-hooks ./src \
--extensions=ts,tsx \
--parser=tsx \
--run-prettier # optional, runs prettier on the transformed files