{"id":8685,"date":"2023-09-15T13:01:19","date_gmt":"2023-09-15T13:01:19","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8685"},"modified":"2024-06-27T10:36:38","modified_gmt":"2024-06-27T10:36:38","slug":"animate-from-left-to-right-and-right-to-left-with-transform-translatex-in-react-native","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react-native\/animate-from-left-to-right-and-right-to-left-with-transform-translatex-in-react-native","title":{"rendered":"Animate From Left to Right and Right to Left with Transform TranslateX in React Native"},"content":{"rendered":"<p>\/\/ Defining the properties for the CardCashout component<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import React, { FC, useEffect, useRef } from 'react';\r\nimport { Animated, Text, Easing } from 'react-native';\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">interface CardCashoutProps {\r\n  openCard: boolean; \/\/ A boolean prop that determines whether the card is open or not\r\n}\r\n<\/pre>\n<p>\/\/ Creating the CardCashout functional component<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const CardCashout: FC&lt;CardCashoutProps&gt; = ({ openCard }) =&gt; {\r\n<\/pre>\n<p>\/\/ Using the useRef hook to maintain a persistent reference to the animated value<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const animationRef = useRef(new Animated.Value(openCard ? 0 : 222)).current;\r\n<\/pre>\n<p>\/\/ Using the useEffect hook to manage animation and reactivity<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">useEffect(() =&gt; {\r\n<\/pre>\n<p>\/\/ Defining the target value for the animation based on the &#8216;openCard&#8217; prop<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const targetValue = openCard ? 0 : 222;\r\n<\/pre>\n<p>\/\/ Choosing the easing function for the animation based on the &#8216;openCard&#8217; prop<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const easingFunction = openCard ? Easing.inOut(Easing.ease) : Easing.out(Easing.ease);\r\n<\/pre>\n<p>\/\/ Configuring the animation using the Animated.timing method<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">  Animated.timing(animationRef, {\r\n    toValue: targetValue, \/\/ Setting the target value for the animation\r\n    duration: 5000, \/\/ Setting the duration of the animation in milliseconds\r\n    easing: easingFunction, \/\/ Applying the chosen easing function\r\n    useNativeDriver: true, \/\/ Using the native driver for improved performance\r\n  }).start(); \/\/ Starting the animation\r\n\r\n}, [openCard, animationRef]); \/\/ Dependency array to track changes in 'openCard' and 'animationRef'<\/pre>\n<p>\/\/ Returning the JSX representing the CardCashout component<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"> return (\r\n    &lt;Animated.View style={{ transform: [{ translateX: animationRef }] }}&gt;\r\n      &lt;Text&gt;CardCashout&lt;\/Text&gt;\r\n    &lt;\/Animated.View&gt;\r\n  );\r\n};\r\n\r\nexport default CardCashout;\r\n<\/pre>\n<p>In this code, we have a React Native functional component called CardCashout that facilitates animating a card&#8217;s appearance from left to right and vice versa. Let&#8217;s break down the key aspects of the code:<\/p>\n<p><strong>Component Properties:<\/strong> The component takes a single property openCard, which is a boolean that indicates whether the card should be open or not.<\/p>\n<p><strong>Animated Value:<\/strong> The useRef hook is employed to create a persistent reference named animationRef to the Animated.Value. This helps maintain the value between re-renders and avoids unnecessary reinitialization.<\/p>\n<p><strong>Animation Logic:<\/strong> The useEffect hook is utilized to manage the animation behavior based on changes in the openCard prop. It calculates the target value for the animation and selects an appropriate easing function (smooth acceleration and deceleration). Then, it configures the animation parameters using the Animated.timing method.<\/p>\n<p><strong>JSX Rendering:<\/strong> The component returns JSX representing an Animated.View. The transform style property is used to apply the animation to the horizontal translation (translateX) of the card. The value is set to the animationRef, which reflects the animated value.<\/p>\n<p>By combining these elements, the CardCashout component effectively animates the card&#8217;s transition from left to right and vice versa when the openCard prop changes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Defining the properties for the CardCashout component import React, { FC, useEffect, useRef } from &#8216;react&#8217;; import { Animated, Text, Easing } from &#8216;react-native&#8217;; interface CardCashoutProps { openCard: boolean; \/\/ A boolean prop that determines whether the card is open or not } \/\/ Creating the CardCashout functional component const CardCashout: FC&lt;CardCashoutProps&gt; = ({ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-8685","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8685"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8685"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8685\/revisions"}],"predecessor-version":[{"id":9752,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8685\/revisions\/9752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8883"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}