Advertisement
Guest User

app.js

a guest
Feb 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const App = () => {
  3.   const allImages = [];
  4.   const positions = new Animated.ValueXY();
  5.   const dimensions = new Animated.ValueXY();
  6.   var viewRef = null;
  7.  
  8.   const [state, setState] = React.useState({
  9.     activeImage: null,
  10.   });
  11.  
  12. =============CALLED ON BUTTON CLICK =======================
  13.   const openImage = index => {
  14.     // we are going to store the image position, dimensions here
  15.     allImages[index].measure((x, y, width, height, pageX, pageY) => {
  16.       // we will store the old coords here...
  17.       positions.setValue({
  18.         x: pageX,
  19.         y: pageY,
  20.       });
  21.       dimensions.setValue({
  22.         x: width,
  23.         y: height,
  24.       });
  25.     });
  26.  
  27.     setState({activeImage: IMAGES[index]});
  28.   };
  29.  
  30.   var animatedImageStyle = React.useRef({});
  31.  
  32.   React.useEffect(() => {
  33.     console.log('1 activeImage', state.activeImage);
  34.     if (state.activeImage) {
  35.       console.log(
  36.         '1 animation style hook called',
  37.         JSON.stringify(positions),
  38.         JSON.stringify(dimensions),
  39.       );
  40.       animatedImageStyle.current = {
  41.         width: dimensions.x,
  42.         height: dimensions.y,
  43.         top: positions.y,
  44.         left: positions.x,
  45.       };
  46.     }
  47.   }, [positions.x, dimensions.x]);
  48.  
  49.   React.useEffect(() => {
  50.     // this is ref to our destination view
  51.     console.log('2 activeImage', state.activeImage);
  52.     if (state.activeImage) {
  53.       console.log(
  54.         '2 animation hook is being called',
  55.         JSON.stringify(positions),
  56.         JSON.stringify(dimensions),
  57.       );
  58.       // viewRef.measure((x, y, width, height, pageX, pageY) => {
  59.       Animated.parallel([
  60.         ...
  61.       ]).start(() => {
  62.         console.log('3 transformation ended');
  63.       });
  64.     }
  65.     // });
  66.   }, [positions.x, dimensions.x]);
  67.  
  68.   console.log('NONE root', animatedImageStyle.current);
  69.  
  70.   return (  
  71.     ...
  72.     //returns with openImage handler use
  73.     ...
  74.   );
  75. };
  76.  
  77. export default App;
  78.  
  79. // OUTPUT
  80.  
  81. NONE root {}
  82.  LOG  1 activeImage null
  83.  LOG  2 activeImage null
  84.  LOG  NONE root {}
  85.  LOG  1 activeImage {"id": 2, "url": 2}
  86.  LOG  1 animation style hook called {"x":0,"y":0} {"x":0,"y":0}
  87.  LOG  2 activeImage {"id": 2, "url": 2}
  88.  LOG  2 animation hook is being called {"x":0,"y":0} {"x":0,"y":0}
  89.  LOG  3 transformation ended
  90.  
  91. // EXPECTED
  92.  
  93. ...
  94.  LOG  1 animation style hook called {"x":123123,"y":123123} {"x":123123,"y":123123}
  95.  LOG  2 activeImage {"id": 2, "url": 2}
  96.  LOG  2 animation hook is being called {"x":123123,"y":123123} {"x":123123,"y":123123}
  97. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement