React-native-reanimated: Is cond() work unexpected?

Created on 26 May 2020  路  1Comment  路  Source: software-mansion/react-native-reanimated

Description

When run below code i get this output:

render home
equal values
not equal values

What am i missing?

Code

// code goes here
import React, {useEffect} from 'react';
import {View} from 'react-native';
import {cond, eq, Value} from 'react-native-reanimated';

const val = new Value(10);
const val2 = new Value(5);

const Home = () => {
  useEffect(() => {
    cond(
      eq(val, val2),
      console.log('equal values'),
      console.log('not equal values'),
    );
  }, []);
  console.log('render home');

  return <View style={{flex: 1}}></View>;
};
export default Home;

Package versions

鉂換uestion

Most helpful comment

Hi @erkangozukucuk,

it seems you're mixing reanimated nodes with JS code, so the result may be surprising.

It works as expected, because you use cond node (which is a function from JS side), so JS evaluates arguments:

  • eq evaluates to eq node
  • both console.logs evaluates to undefined and prints text on the console as a side effect

Try it yourself by adding:

const some_function = () => {
  console.log('eq');
  return 5;
};

and changing nodes to:

cond(eq(val, val2), some_function(), console.log('not equal values'));

If you have any further concerns don't hesitate to reopen.

>All comments

Hi @erkangozukucuk,

it seems you're mixing reanimated nodes with JS code, so the result may be surprising.

It works as expected, because you use cond node (which is a function from JS side), so JS evaluates arguments:

  • eq evaluates to eq node
  • both console.logs evaluates to undefined and prints text on the console as a side effect

Try it yourself by adding:

const some_function = () => {
  console.log('eq');
  return 5;
};

and changing nodes to:

cond(eq(val, val2), some_function(), console.log('not equal values'));

If you have any further concerns don't hesitate to reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afctemka picture afctemka  路  3Comments

wasim-abuzaher picture wasim-abuzaher  路  3Comments

levibuzolic picture levibuzolic  路  3Comments

sa8ab picture sa8ab  路  3Comments

nextriot picture nextriot  路  3Comments