rive-app / rive-app/rive-react-native

Setting initial state

Open
#115 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
783
Forks
81
Avg merge
3d 6h
Merged PRs (30d)
3

Description

Description

I'm trying to make an Avatar component with Rive. There may be several avatars on the screen at once, each with different outfits. This requires hiding and showing layers via inputs on state machines. The issue with this approach is that there is a "flash of unstyled content". Each avatar is shown for 1 frame in a default state after which the ref is initialized and I can trigger the state machines to instantly show/hide parts to make it look appropriate.

It would be ideal if there was a way to declaratively specify states so that the animation can be loaded in the correct state instead of needing to transition first from a default state.

I can probably work around this by having the root be 0% opacity until I trigger the state update after load but this seems like a sub-optimal solution.

Provide a Repro
//this results in a default state being shown on frame1 after which the "real" state is imperatively set via useEffect
//it would be better if we could specify input states in a declarative way via props so animations can load initialized to correct values
const RiveInstance = observer(function RiveInstance({ num }: { num: number }) {
    const riveRef = React.useRef<RiveRef>(null)

    useEffect(() => {
        if (riveRef.current) {
            riveRef.current.setInputState("ShowHatHeavy", "HatHeavyVariant", (num % 5) + 1)
            riveRef.current.setInputState("ShowShield", "ShieldVariant", (num % 5) + 1)
        }
    }, [num, riveRef.current])

    return (
        <View key={num}>
            <Rive autoplay={false} ref={riveRef} style={{ width: 50, height: 50 }} resourceName={"test_avatar"} />
        </View>
    )
})

const TestRive = observer(function TestRive() {
    return (
        <ScrollView style={{ width: "100%", height: 150 }}>
            {_.range(10).map((i) => (
                <RiveInstance num={i} key={i} />
            ))}
        </ScrollView>
    )
})
Source .riv/.rev file

avatar_test.rev.zip

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Rive component's initialization and ref-based setInputState calls shown in the reproduction, then identify how state-machine inputs are applied during loading. Done means callers can provide initial input values declaratively and the avatar renders in the requested state without showing the default state for a frame.

Written by the indexing model from the issue text.

Assessment

Tech stack
react-native, typescript
Domain
mobile
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.