Author: saqibkhan

  • Give a quick overview of the life cycle of a React component?

    Below image summarise the lifecycle methods exposed by react component and the order in which lifecycle methods are being called by React.

    life cycle of a React component

    React 16 was a major release for ReactJS. With this version react has marked some of the lifecycle methods like componentWillRecieveProps, ComponentWillUpdate as deprecated. In a later release of React, the framework will be removing these methods. In place of deprecated methods, they added a few new lifecycle methods. The above picture shows the lifecycle method of a component. We can divide the lifecycle methods of the component into three categories.

    • Mounting: This includes the beginning phase of the component. The first constructor of the component gets invoked. Constructor receives props as an argument from the parent component. Constructor is the place where we define an initial state of a component. After constructor and before render, lifecycle method getDrivedStateFromProps is invoked, it returns the state object or null to avoid the render. Then Render method is invoked which renders the JSX. Then at the end, componentDidMount is invoked.
    • Updating: A component gets an update via a change in state or change in props. In both of these cases, the getDrivedStateFromProps method is invoked first where we get props and state as an argument and returns updated state. Then shouldComponentUpdate methods are invoked. If this method returns true then the only component gets the update otherwise component update is aborted. Then render method is called and after this getSnapshotBeforeUpdate is called. In the end, the componentDidUpdate method is called
    • Unmounting: componentWillUnmount lifecycle method is invoked before the unmounting of the component takes place.
  • What is the difference between a functional component and a class-based component?

    Though we can implement in both functional and class-based way, there are few fundamental differences between the two of them:

    1. Functional component can’t have stated ( before React hooks era ). It renders component only based on props passed. A class-based component can have a local state which can be helpful in building a bigger component
    2. Functional component access directly via props argument passed. In a class-based component, props are accessible via this.props.
    3. A class-based component is a more complex structure. It is an instance of a class derived from React. Component class. The class must implement a render() member function which returns a React component to be rendered
    4. The class-based component can also use lifecycle methods but functional component can’t use lifecycle methods.
    5. Functional components are a very light weighted component in comparison to class-based react component
  • What are the ways to write react components?

    here are two ways of writing a react component

    • Functional component
      • It uses a simple function which returns the JSX
    • Class-based component
      • It used the class keyword introduced in ES6. it implements render lifecycle method which returns the JSX.

    Example of functional component

    Functional component

    Example of class-based component

    Class-based component
    /* Class based component */
    import React, { Component } from 'react';
    import { Text } from 'react-native';
    
    export default class Greeting extends Component {
        render() {
            return (
                <Text>Hello {this.props.name} !</Text>
            );
        }
    }
    
    /* Functional component */
    import React, { Component } from 'react';
    import { Text } from 'react-native';
    
    export default function Greeting(props) {
            return (
                <Text>Hello {props.name} !</Text>
            );
    }

    cases are known as a presentational or dumb component. The functional component gets information via props from its parent component and renders the information. You can see that the functional component accepts props as an argument. ES6 arrow function also is used to define a functional component. The functional component doesn’t store any state and also doesn’t override any lifecycle method of React component. The class-based component has the ability to store state within the component and based on its state value behave differently. The latest version of React 16.8 has included a new feature called React hooks by which we can add state in a functional component.

  • What all CSS properties are not supported by React native?

    Though most of the CSS properties works in the same way in React native still there are few differences in CSS property values between a browser and React native:

    1. Only two values of CSS property display (none, flex) is supported in React native. display none is used to hide an element.
    2. aspectRatio is non-CSS property supported by React Native. we can use aspect ratio to give a size to an element. if either width or height is specified along with aspect ratio then other parameters will be calculated and applied based on aspect ratio value
    3. marginHorizontal has the same effect as setting both marginLeft and marginRight. Similar to marginHorizontal, marginVertical has the same effect as setting both marginTop and marginBottom.
    4. The default value of the position in React native is relative instead of static. so absolute positioning is always just relative to the parent.

    Apart from the above difference, most of the CSS properties work exactly the same as they work in a browser.

  • How can we change the default behaviour of the main axis and cross axis of flexbox layout?

    By default, the main axis is the vertical axis and cross axis is the horizontal axis in React native. Since justifyContent and alignItems property works based on the main axis and cross axis, so justifyContent will align flex items vertically and alignItems will layout flex item horizontally. This default value of the main axis and cross axis can be changed by changing flexDirection property. If we set flexDirection to row in the flex container then the main axis will become horizontal axis and cross axis will become vertical axis. 

    On changing default behaviour via flexDirection CSS property, the behaviour of alignItems and justifyContent will also get switched. The Concept of flexDirection is also present on the web. Flexbox is implemented as a single direction layout technique and this direction is managed by flexDirection CSS property. Apart from row and column, the flexDirection property has two more values: row-reverse and column-reverse. As the name suggests, the direction will remain horizontal and vertical but the direction will get changed.

    Transitions are an important feature of mobile devices. So, this is one of the most frequently asked React Native interview questions and answers for senior developer in recent times.

  • How does alignItems flexBox property works?

    You can think of alignItems as justifyContent behaviour for cross axis. Cross-axis in case if React native is the horizontal axis. CSS alignItems property sets the alignSelf value on all direct children as a group. In Flexbox, it controls the alignment of items on the cross axis. By default, the cross axis is a horizontal axis in case of React native. We can use this property to layout elements in the flex container. The alignItems property supports following values: stretch|center|flex-start|flex-end|baseline|initial|inherit;  Let me explain alignItems values:

    • flex-start: this is the default value for alignItems. It means that flex items will start from the left and evenly distributed horizontally.
    • Flex-end: this is just the opposite behaviour of flex-start. Elements start rendering from the right and go up to the left end.
    • Center: Items will be placed in the middle
    • Space-between: elements are evenly distributed along the cross axis (horizontal  axis)
    • Space-around: flex items will be evenly distributed with equal space around them
  • How does justifyContent flexBox property work?

    JustifyContent property aligns the flexible container’s items when the items do not use all available space on the main axis. By default, the main axis is a vertical axis in case of React native. Which means justifyContent property aligns child elements of flex parent vertically in React native.  We can use this property to layout elements in a flex container. JustifyContent supports the following values: flex-start|flex-end|centre|space-between|space-around|initial|inherit; It also apply some control over the alignment of items when they overflow the line. Let me explain JustifyContent’s values:

    • flex-start: this is the default value for justifyContent. It means that flex items will start from the top and evenly distributed vertically.
    • Flex-end: this is just the opposite behaviour of flex-start. Elements start rendering from the bottom
    • Center: Items will be placed in the middle
    • Space-between: elements are evenly distributed along the main axis (vertical axis)
    • Space-around: flex items will be evenly distributed with equal space around them
  • What are the differences between Flexbox in browser and Flexbox in React Native?

    CSS Flexbox is used to design a responsive layout easily without using float or position CSS property. Float and position values were used to build any type of UI which are not very easy. Flexbox is added in CSS3. Flexbox is designed to provide a consistent layout on different screen sizes. You will normally use a combination of flexDirection, alignItems, and justify-content to achieve the right layout. 

    There are few differences in default values of few flex based CSS properties between React Native and on the browser. The default value of flexDirection is a row in the web but in case of React native, its default value is a column. Also, flex parameter only supports a single number in React native. Flexbox alignItems has few more values like flex-start which start aligning element from start and opposite of this is flex-end which places the first child element at the end. Similar to alignItems, justifyContents also have values which behaves where much similar to flexbox behaviour in a web browser.

  • How CSS flexbox is used to layout a React Native UI

    By default, the View component in React Native has its display property set to flex. Flex is a CSS property used to expand and shrink components dynamically based on available space. Setting flex: 1 on a component makes it take up all the available space. If a parent element has flex: 1, its child elements will evenly distribute the available space among themselves. 

    The width of a child element can be adjusted by assigning it a higher flex value; the larger the value, the more space the component will take relative to its siblings. However, for a component to expand, its parent must have dimensions greater than zero. If the parent element’s dimensions are not set, the flex property will not work, and the element will not be visible on the UI. 

    In addition to the flex property, align-items and justify-content CSS properties are also used to design mobile UIs in React Native. 

  • How do you handle elements size in React Native?

    React native follows the box-model concept of CSS. The size of the element is calculated based on the size of content, padding, border, margin. The simplest way to set the size of an element is to set width and height CSS property for an element. All dimensions in React Native is unitless and represent density-independent pixels. By setting fixed height and width, the element will look exactly the same size on different screen sizes. But there is an instance where you want to give the width and height of an element in percentage. 

    Directly use of percentage is not supported in React native but React native does give a dimension module which can be used to give width in percentage. Dimension module gives the width and height of the mobile device. This information can be used to set the style of an element in runtime. Below is the example of how to use Dimension module from React native:

    Importing module from React Native:
    import { Dimension } from ‘react-native’;
    Figure out width and height of the device:
    const deviceWidth = Dimension.get(“window”).width;
    const deviceHeight = Dimension.get(“window”).height;
    Calculate the style value:
    Width: deviceWidth*<percentageValue>/100

    But the simplest way is by setting width and height CSS for an element.