How to transform an object in React Native (skew, scale, transform, rotate)

Introduction :

React native provides style properties to do 2D or 3D transformation of an object like skew, rotate, scale etc. But note that it will not change the layout size defined for that object. If you have any other objects nearby, it will overlap. To prevent this, you need to add margin or padding to the object.

Props :

Only one props is used for transformation, transform :

It takes an array of objects of the following types :

  1. perspective (number)
  2. rotate (string)
  3. rotateX (string)
  4. rotateY (string)
  5. rotateZ (string)
  6. scale (number)
  7. scaleX (number)
  8. scaleY (number)
  9. translateX (number)
  10. translateY (number)
  11. skewX (string)
  12. skewY (string)

For rotate, degree and radian is used and for skew, degree is used. deg is used for degree like 10deg and rad is used for radian like .5rad.

Example program :

Let’s take a look at the below example :

import React from 'react';
import {StyleSheet, View} from 'react-native';

export default function ExampleProgram() {
  return (
    <View style={styles.container}>
      <View style={styles.square} />
      <View
        style={[
          styles.square,
          {
            transform: [{scale: 2}],
          },
        ]}
      />
      <View
        style={[
          styles.square,
          {
            transform: [{translateX: 20}],
          },
        ]}
      />
      <View
        style={[
          styles.square,
          {
            transform: [{rotateZ: '20deg'}],
          },
        ]}
      />
      <View
        style={[
          styles.square,
          {
            transform: [{skewX: '30deg'}],
          },
        ]}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    margin: 12,
  },
  square: {
    height: 50,
    width: 50,
    backgroundColor: 'red',
    margin: 30,
  },
});

Here, I am showing scale, translateX, rotateZ and skewX properties. You can also pass more than one property in the array. It will produce the below output :

React Native transform