Author: saqibkhan

  • ReactWave

    Setting Up Your Environment

    If you haven’t already set up a React project, you can use Create React App:

    bashCopy codenpx create-react-app react-wave
    cd react-wave
    

    Step 2: Create a Wave Component

    We will create a component that renders a wave using SVG. Let’s create a new file called Wave.js in the src folder.

    1. Create Wave.js

    jsxCopy code// src/Wave.js
    import React from 'react';
    
    const Wave = ({ amplitude, frequency, speed }) => {
      const wavePath = (t) => {
    
    const y = amplitude * Math.sin(frequency * t);
    return ${t},${y + 100}; // Adjust y offset to position the wave in the SVG
    }; const points = Array.from({ length: 200 }, (_, i) => wavePath(i * speed)).join(' '); return (
    <svg width="100%" height="200" viewBox="0 0 200 200">
      <polyline
        points={points}
        fill="none"
        stroke="blue"
        strokeWidth="2"
        strokeLinecap="round"
      />
    </svg>
    ); }; export default Wave;

    Step 3: Integrate Wave Component in App

    Now, let’s use the Wave component in App.js and add some basic state management to animate it.

    1. Update App.js

    jsxCopy code// src/App.js
    import React, { useState, useEffect } from 'react';
    import Wave from './Wave';
    import './App.css';
    
    const App = () => {
      const [amplitude, setAmplitude] = useState(20);
      const [frequency, setFrequency] = useState(0.1);
      const [speed, setSpeed] = useState(0.2);
      const [offset, setOffset] = useState(0);
    
      useEffect(() => {
    
    const interval = setInterval(() => {
      setOffset((prevOffset) => (prevOffset + speed) % 200);
    }, 100);
    return () => clearInterval(interval);
    }, [speed]); return (
    <div className="App">
      <h1>Wave Animation</h1>
      <Wave amplitude={amplitude} frequency={frequency} speed={offset} />
      <div className="controls">
        <label>
          Amplitude:
          <input
            type="number"
            value={amplitude}
            onChange={(e) => setAmplitude(e.target.value)}
          />
        </label>
        <label>
          Frequency:
          <input
            type="number"
            value={frequency}
            onChange={(e) => setFrequency(e.target.value)}
          />
        </label>
        <label>
          Speed:
          <input
            type="number"
            value={speed}
            onChange={(e) => setSpeed(e.target.value)}
          />
        </label>
      </div>
    </div>
    ); }; export default App;

    Step 4: Add Basic Styles

    Add some basic styles in App.css to make it look nicer:

    cssCopy code/* src/App.css */
    .App {
      text-align: center;
      padding: 20px;
    }
    
    .controls {
      margin-top: 20px;
    }
    
    .controls label {
      margin: 0 10px;
    }
    

    Step 5: Run Your Application

    Start your application using:

    bashCopy codenpm start
    

    Summary

    You now have a simple React application that features a wave animation! Users can control the amplitude, frequency, and speed of the wave through input fields. This serves as a basic introduction to creating animations with SVG in React.

    Feel free to experiment with different values and enhance the app with more features, such as color customization or saving configurations!

  • NativeNest

    Setting Up Your Environment

    Make sure you have Node.js and npm installed. You can set up a new React Native project using Expo, which simplifies the process:

    1. Install Expo CLI if you haven’t already:bashCopy codenpm install -g expo-cli
    2. Create a new project:bashCopy codeexpo init my-native-app cd my-native-app
    3. Choose a template (for example, the “blank” template).

    Step 2: Create Basic Components

    We’ll create two main components: ItemList and AddItem.

    1. Create ItemList.js

    In the components folder (create it if it doesn’t exist), create a new file called ItemList.js:

    jsxCopy code// components/ItemList.js
    import React from 'react';
    import { View, Text, FlatList } from 'react-native';
    
    const ItemList = ({ items }) => {
      return (
    
    <FlatList
      data={items}
      keyExtractor={(item, index) => index.toString()}
      renderItem={({ item }) => (
        <View style={{ padding: 10 }}>
          <Text>{item}</Text>
        </View>
      )}
    />
    ); }; export default ItemList;

    2. Create AddItem.js

    Now create another file called AddItem.js:

    jsxCopy code// components/AddItem.js
    import React, { useState } from 'react';
    import { View, TextInput, Button } from 'react-native';
    
    const AddItem = ({ onAdd }) => {
      const [inputValue, setInputValue] = useState('');
    
      const handleSubmit = () => {
    
    if (inputValue) {
      onAdd(inputValue);
      setInputValue('');
    }
    }; return (
    <View style={{ flexDirection: 'row', padding: 10 }}>
      <TextInput
        style={{
          borderWidth: 1,
          borderColor: '#ccc',
          flex: 1,
          marginRight: 10,
          padding: 10,
        }}
        placeholder="Add new item"
        value={inputValue}
        onChangeText={setInputValue}
      />
      <Button title="Add" onPress={handleSubmit} />
    </View>
    ); }; export default AddItem;

    Step 3: Main Application Component

    Now, let’s connect everything in App.js.

    jsxCopy code// App.js
    import React, { useState } from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    import ItemList from './components/ItemList';
    import AddItem from './components/AddItem';
    
    const App = () => {
      const [items, setItems] = useState([]);
    
      const addItem = (item) => {
    
    setItems([...items, item]);
    }; return (
    <View style={styles.container}>
      <Text style={styles.title}>My Item List</Text>
      <AddItem onAdd={addItem} />
      <ItemList items={items} />
    </View>
    ); }; const styles = StyleSheet.create({ container: {
    flex: 1,
    padding: 20,
    backgroundColor: '#fff',
    }, title: {
    fontSize: 24,
    fontWeight: 'bold',
    marginBottom: 20,
    }, }); export default App;

    Step 4: Run Your Application

    Start your application using Expo:

    bashCopy codeexpo start
    

    This will open a new tab in your browser where you can run the app on an emulator or your physical device using the Expo Go app.

    Step 5: Explore and Enhance

    You now have a basic React Native application! You can enhance it by adding features like:

    • Removing items
    • Editing items
    • Styling with more complex designs
    • Persisting data using AsyncStorage

    Summary

    In this tutorial, you created a simple React Native app that allows users to add items to a list. You learned how to manage state and create reusable components in React Native.

  • Reactron

    Setting Up Your Environment

    Make sure you have Node.js and npm installed. You can create a new React app using Create React App by running:

    bashCopy codenpx create-react-app my-app
    cd my-app
    

    Step 2: Creating the Basic Components

    We’ll create a simple application with two main components: ItemList and AddItem.

    1. Create ItemList.js

    In the src folder, create a new file called ItemList.js:

    jsxCopy code// src/ItemList.js
    import React from 'react';
    
    const ItemList = ({ items }) => {
      return (
    
    <ul>
      {items.map((item, index) => (
        <li key={index}>{item}</li>
      ))}
    </ul>
    ); }; export default ItemList;

    2. Create AddItem.js

    Now create another file called AddItem.js:

    jsxCopy code// src/AddItem.js
    import React, { useState } from 'react';
    
    const AddItem = ({ onAdd }) => {
      const [inputValue, setInputValue] = useState('');
    
      const handleSubmit = (e) => {
    
    e.preventDefault();
    if (inputValue) {
      onAdd(inputValue);
      setInputValue('');
    }
    }; return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
        placeholder="Add new item"
      />
      <button type="submit">Add</button>
    </form>
    ); }; export default AddItem;

    Step 3: Main Application Component

    Now, let’s tie everything together in App.js.

    jsxCopy code// src/App.js
    import React, { useState } from 'react';
    import ItemList from './ItemList';
    import AddItem from './AddItem';
    
    const App = () => {
      const [items, setItems] = useState([]);
    
      const addItem = (item) => {
    
    setItems([...items, item]);
    }; return (
    <div>
      <h1>My Item List</h1>
      <AddItem onAdd={addItem} />
      <ItemList items={items} />
    </div>
    ); }; export default App;

    Step 4: Run Your Application

    Now you can start your application. In your terminal, run:

    bashCopy codenpm start
    

    Step 5: Explore and Enhance

    You now have a basic React application that allows you to add items to a list. You can enhance it by adding features like:

    • Removing items
    • Editing items
    • Styling with CSS
    • Persisting data to local storage

    Summary

    This tutorial introduced you to the basics of React by building a simple item list app. You learned how to manage state and create reusable components. As you get comfortable, try expanding the app with more features and styling!

  • How does the alignItems flexBox property works?

    You can think of alignItems as justifyContent behaviour for cross-axis. Cross-axis in the 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 horizontal in the 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 align items values: 

    1. flex-start: this is the default value for alignItems. It means that flex items will start from the left and be evenly distributed horizontally. 
    2. Flex-end: this is just the opposite behavior of flex-start. Elements start rendering from the right and go up to the left end. 
    3. Center: Items will be placed in the middle 
    4. Space-between: elements are evenly distributed along the cross-axis (horizontal axis) 
    5. Space-around: flex items will be evenly distributed with equal space around them 
  • How does the 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 vertical in the case of React native. This 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 applies some control over the alignment of items when they overflow the line. Let me explain JustifyContent’s values: 

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

    CSS Flexbox is used to design responsive layouts easily, without relying on float or position CSS properties, which can be cumbersome. Introduced in CSS3, Flexbox ensures consistent layouts across different screen sizes. 

    In React Native, you typically use a combination of flexDirection, alignItems, and justifyContent to achieve the desired layout. There are some differences in the default values of certain flex-based CSS properties between React Native and web browsers: 

    • flexDirection: Defaults to row on the web, but to column in React Native. 
    • flex: In React Native, the flex parameter supports only a single number. 

    Flexbox properties like alignItems and justifyContent help position elements: 

    • alignItems: Values like flex-start align elements from the start, while flex-end places the first child element at the end. 
    • justifyContent: Similar to alignItems, it positions elements along the main axis. 

    These properties allow for easy and flexible layout designs, ensuring responsive and adaptive UIs across different devices. 

  • How do you handle element size in React Native? 

    React Native follows the box model concept of CSS, where the size of an element is determined by its content, padding, border, and margin. The simplest way to set an element’s size is by specifying the width and height CSS properties. In React Native, all dimensions are unitless and represent density-independent pixels, ensuring consistent element sizes across different screen sizes when using fixed values. 

    However, if you need to set dimensions as a percentage, React Native does not directly support this. Instead, it provides the Dimensions module, which gives the width and height of the mobile device. This information can be used to dynamically set an element’s style at runtime, allowing for responsive design by calculating percentage-based dimensions. 

    Below is an example of how to use the Dimension module from React native: 

    Importing module from React Native: 

    import { Dimension } from ‘react-native’; 

    Figure out the 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. 

  • What is the state in the React component?

    The state is another way apart from props by which we can modify a React component. React component’s state value changes in the life cycle of the component, unlike props. We should not directly change the state value of the react component. React framework gives the setState method by which the state of a component should be changed. 

    import React from 'react'; 
    import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'; 
     
    class Counter extends React.Component { 
      state = { count: 0 }; 
      increment = () => this.setState({count: this.state.count + 1}); 
      decrement = () => this.setState({count: this.state.count - 1}); 
      render() { 
        return ( 
          <View style={styles.container}> 
            <TouchableOpacity onPress={this.decrement}> 
              <Text style={styles.text}>-</Text> 
            </TouchableOpacity> 
            <Text style={styles.text}>{this.state.count}</Text> 
            <TouchableOpacity onPress={this.increment}> 
              <Text style={styles.text}>+</Text> 
            </TouchableOpacity> 
          </View> 
        ); 
      } 
    }; 
    const styles = StyleSheet.create({ 
      container: { 
        flexDirection: 'row', 
        borderRadius: 5, 
        borderWidth: 1, 
        borderColor: '#1a91b8', 
        padding: 5, 
        backgroundColor: '#eaf7fd' 
      }, 
      text: { 
        color: '#015169', 
        fontWeight: 'bold', 
        fontSize: 20, 
        padding: 15 
      } 
    }); 
    export default Counter; 
     

    The above code is an implementation of a counter component. This component has a state count whose value is changed by clicking on the plus (+) and minus (-) buttons. As the state count of this component is changing the number displayed on UI is also changing. Whenever we create any component in React, first we divide the screen in the component and then decide what state a component should have. The beauty of this component is that it is an independent component and can be used anywhere in the application. That is how we make several fully functional components in React to build the complete application. 

  • How do you integrate third-party libraries with React Native?

    Integrating third-party libraries involves: 

    • Installation: Use npm or yarn to install the library. 
    • LinkingUse react-native link or manually link the library if needed. 
    • Configuration: Follow the library’s setup instructions for configuration and usage. 

    These steps ensure the library is correctly integrated and functional within the React Native app. 

  • What are Native Modules, and how do you use them in React Native?

    Native Modules allow integrating custom native code into a React Native app: 

    • Custom Code: Write native code in Java (Android) or Objective-C/Swift (iOS). 
    • JavaScript BridgeExpose native functionalities to JavaScript through the bridge. 
    • IntegrationUse NativeModules to access and utilize native modules in the React Native app. 

    Native Modules provide flexibility to leverage platform-specific features and optimize performance.