Replies: 3 comments 3 replies
-
You might think about having the level machine spawn new actors for each character based on the current level. Extract the logic from React into the machine. |
Beta Was this translation helpful? Give feedback.
-
@Blacktiger I've been a little afraid of giving up lifecycle control, so I haven't used |
Beta Was this translation helpful? Give feedback.
-
Yesterday I learned that you actually can break this first rule of hooks without any issues, so long as the hook order remains invariant. For config cases like mine, this is awesome news. I tried using |
Beta Was this translation helpful? Give feedback.
-
I'm making a game that has a lot of characters. Each character has:
Each Level of the game uses only a handful of the characters. e.g. Level 4 uses character1, character6, and character19. Level 5 uses character2 and character8.
My root component (Level) initializes all the characters' machines/services, and passes them down to their corresponding React component. This way the Level can send messages to all the services.
Level is using
useMachine(...)
to create the machines/interpreters for the characters. Trouble is, with React's hook limitation (no conditionals), Level ends up creating all characters' machines & interpreters, even for characters that aren't used during that Level. This feels wasteful and could impact the game's performance.This is a React limitation that doesn't have a great workaround as far as I can tell. Maybe
useMachine
could sometimes be a noop/return undefined, but that might not be fun for TS users...Another thought would be to not use
useMachine
, and just useMachine()
directly in my own custom hook. I'd probably need to reimplement all the cases thatuseMachine
handles though. UnlessuseMachine
's logic could be pulled out into a regular function util or something. Just thinking out loud here.Beta Was this translation helpful? Give feedback.
All reactions