Skip to content

Commit 0fc363e

Browse files
committed
refactor: move syncInputs function inside useEffect
1 parent 79ca848 commit 0fc363e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/hooks/useStateMachineInputs.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@ export default function useStateMachineInputs(
2020
) {
2121
const [inputs, setInputs] = useState<StateMachineInput[]>([]);
2222

23-
const syncInputs = useCallback(() => {
24-
if (!rive || !stateMachineName || !inputNames) return;
23+
useEffect(() => {
24+
const syncInputs = () => {
25+
if (!rive || !stateMachineName || !inputNames) return;
2526

26-
const riveInputs = rive.stateMachineInputs(stateMachineName);
27-
if (!riveInputs) return;
27+
const riveInputs = rive.stateMachineInputs(stateMachineName);
28+
if (!riveInputs) return;
2829

29-
// To optimize lookup time from O(n) to O(1) in the following loop
30-
const riveInputLookup = new Map<string, StateMachineInput>(
31-
riveInputs.map(input => [input.name, input])
32-
);
30+
// To optimize lookup time from O(n) to O(1) in the following loop
31+
const riveInputLookup = new Map<string, StateMachineInput>(
32+
riveInputs.map(input => [input.name, input])
33+
);
3334

34-
setInputs(() => {
35-
// Iterate over inputNames instead of riveInputs to preserve array order
36-
return inputNames
37-
.filter(inputName => riveInputLookup.has(inputName.name))
38-
.map(inputName => {
39-
const riveInput = riveInputLookup.get(inputName.name)!;
35+
setInputs(() => {
36+
// Iterate over inputNames instead of riveInputs to preserve array order
37+
return inputNames
38+
.filter(inputName => riveInputLookup.has(inputName.name))
39+
.map(inputName => {
40+
const riveInput = riveInputLookup.get(inputName.name)!;
4041

41-
if (inputName.initialValue !== undefined) {
42-
riveInput.value = inputName.initialValue;
43-
}
42+
if (inputName.initialValue !== undefined) {
43+
riveInput.value = inputName.initialValue;
44+
}
4445

45-
return riveInput;
46-
});
47-
});
48-
}, [inputNames, rive, stateMachineName]);
46+
return riveInput;
47+
});
48+
});
49+
};
4950

50-
useEffect(() => {
5151
syncInputs();
5252
if (rive) {
5353
rive.on(EventType.Load, syncInputs);

0 commit comments

Comments
 (0)