Skip to content
Discussion options

You must be logged in to vote

Hey @Choco-milk-for-u!

1. TypeScript solution

If you use TypeScript, then you can use the type that won't allow .value assignment like this:

function ComponentA() {
  const sv = useSharedValue(0);

  return <ComponentB sv={sv} />;
}

type ComponentBProps = {
  sv: Readonly<SharedValue<number>>;
};

function ComponentB({ sv }: ComponentBProps) {
  useEffect(() => {
    // Writing is not allowed
    sv.value = 1;

    // Reading is fine
    console.log('sv', sv.value);
  }, [sv]);

  return null;
}

It still won't prevent you from assigning the value if you decide to ignore the TS error. For more safety, you can use the following approach.

2. Using Derived Values

Instead of passing a Shared…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Choco-milk-for-u
Comment options

Answer selected by Choco-milk-for-u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants