Skip to content

Commit 9de1488

Browse files
authored
Added console warnings and improved rules for react integration (#190)
1 parent 8897ea7 commit 9de1488

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

packages/interact/rules/full-lean.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,20 @@ The `config` object is an `InteractConfig` containing `interactions` (required),
6262

6363
**React:**
6464

65+
- Wrap the `Interact.create()` call in a `useEffect` hook to prevent it from running on server-side.
66+
- Store the returned instance, and call its `.destroy()` method on the effect's cleanup function.
67+
6568
```ts
69+
import { useEffect } from 'react';
6670
import { Interact } from '@wix/interact/react';
67-
const instance = Interact.create(config);
71+
72+
useEffect(() => {
73+
const instance = Interact.create(config);
74+
75+
return () => {
76+
instance.destroy();
77+
};
78+
}, [config]);
6879
```
6980

7081
**Vanilla JS:**

packages/interact/rules/integration.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ Wrap target elements with `<interact-element>`:
5353

5454
### React
5555

56+
- Wrap the `Interact.create()` call in a `useEffect` hook to prevent it from running on server-side.
57+
- Store the returned instance, and call its `.destroy()` method on the effect's cleanup function.
58+
5659
```typescript
60+
import { useEffect } from 'react';
5761
import { Interact } from '@wix/interact/react';
5862

59-
Interact.create(config);
63+
useEffect(() => {
64+
const instance = Interact.create(config);
65+
66+
return () => {
67+
instance.destroy();
68+
};
69+
}, [config]);
6070
```
6171

6272
Replace target elements with `<Interaction>`:

packages/interact/src/core/Interact.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,19 @@ export class Interact {
220220
}
221221

222222
static getInstance(key: string): Interact | undefined {
223-
return Interact.instances.find((instance) => instance.has(key));
223+
const instance = Interact.instances.find((instance) => instance.has(key));
224+
if (!instance) {
225+
console.warn(`Interact: Instance for key "${key}" not found`);
226+
}
227+
return instance;
224228
}
225229

226230
static getController(key: string | undefined): IInteractionController | undefined {
227-
return key ? Interact.controllerCache.get(key) : undefined;
231+
const controller = key ? Interact.controllerCache.get(key) : undefined;
232+
if (!controller) {
233+
console.warn(`Interact: Controller for key "${key}" not found`);
234+
}
235+
return controller;
228236
}
229237

230238
static setController(key: string, controller: IInteractionController): void {

0 commit comments

Comments
 (0)