-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: add useStateMachineInputs hook #310
base: main
Are you sure you want to change the base?
Changes from 2 commits
ec8c4eb
fa9e7de
2a26db1
fc0c547
f25dc49
79ca848
0fc363e
9a7b3ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { EventType, Rive, StateMachineInput } from '@rive-app/canvas'; | ||
|
||
/** | ||
* Custom hook for fetching multiple stateMachine inputs from a rive file. | ||
* Particularly useful for fetching multiple inputs from a variable number of input names. | ||
* | ||
* @param rive - Rive instance | ||
* @param stateMachineName - Name of the state machine | ||
* @param inputNames - Names of the inputs | ||
* @returns StateMachineInput[] | null | ||
*/ | ||
export default function useStateMachineInputs( | ||
rive: Rive | null, | ||
stateMachineName?: string, | ||
inputNames?: { | ||
name: string; | ||
initialValue?: number | boolean; | ||
}[] | ||
) { | ||
const [inputs, setInputs] = useState<StateMachineInput[] | null>(null); | ||
|
||
useEffect(() => { | ||
function setStateMachineInput() { | ||
if (!rive || !stateMachineName || !inputNames) { | ||
setInputs(null); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this necessary? It seems the next lines are checking the same and the else statement is also handling the alternate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it doesn't seem necessary to me either, but I had that in because I found the same logic in the |
||
|
||
if (rive && stateMachineName && inputNames) { | ||
const inputs = rive.stateMachineInputs(stateMachineName); | ||
if (inputs) { | ||
const selectedInputs = inputs.filter((input) => | ||
inputNames.some((inputName) => inputName.name === input.name) | ||
); | ||
if (selectedInputs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this validation isn't needed, a filter call will always return an array at this point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! I'll fix that too! |
||
selectedInputs.forEach((input) => { | ||
const targetInputName = inputNames.find(inputName => inputName.name === input.name); | ||
if(targetInputName?.initialValue){ | ||
input.value = targetInputName.initialValue | ||
} | ||
}); | ||
} | ||
setInputs(selectedInputs); | ||
} | ||
} else { | ||
setInputs(null); | ||
} | ||
} | ||
setStateMachineInput(); | ||
if (rive) { | ||
rive.on(EventType.Load, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for safety, the useEffect hook can return a clean up function to clear this callback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I'll add a |
||
// Check if the component/canvas is mounted before setting state to avoid setState | ||
// on an unmounted component in some rare cases | ||
setStateMachineInput(); | ||
}); | ||
} | ||
}, [inputNames, rive, stateMachineName]); | ||
|
||
return inputs; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about removing the union with null and having inputs only be
StateMachineInput[]
?I think an empty array is a simpler api to deal with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that definitely seems like a better option. I'll change that!